




clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user

set graphics on  
 
global path_data_raw "\data\raw"
global path_data_processed "\data\processed"

clear all
set more off



*******************************************
***** 1) Main experiment: Main survey *****
*******************************************

***** Preparing data *****
**************************

cd "$path_data_raw"
insheet using "experimentRN1.csv", names

sort rnid
gen newid=_n
keep rnid newid

cd "$path_data_raw"
save "experimentRN1_IDs.dta",  replace // saving file with new IDs

clear

insheet using "experimentRN1.csv", names

sort rnid
gen newid=_n

rename v10 finished
la var finished "Finished"

gen d_start = substr(v8,9,2)
tab d_start
gen h_start = substr(v8,12,2)
tab h_start
gen m_start = substr(v8,15,2)
tab m_start
gen s_start = substr(v8,18,2)
tab s_start
gen d_end = substr(v9,9,2)
tab d_end
gen h_end = substr(v9,12,2)
tab h_end
gen m_end = substr(v9,15,2)
tab m_end
gen s_end = substr(v9,18,2)
tab s_end


* Earnings entered as open text entry. Fix manually for cases that cannot be automatically destringed:
replace allearnings="1167" if allearnings=="1,167"
replace allearnings="10000" if allearnings=="10,000"|allearnings=="10,000.00"
replace allearnings="100000" if allearnings=="100,000"
replace allearnings="103500" if allearnings=="103,500"
replace allearnings="108000" if allearnings=="108,000"
replace allearnings="11600" if allearnings=="11,600"
replace allearnings="110000" if allearnings=="110,000"
replace allearnings="115000" if allearnings=="115,000"
replace allearnings="12000" if allearnings=="12,000"
replace allearnings="120000" if allearnings=="120,000"
replace allearnings="122000" if allearnings=="122,000"
replace allearnings="124000" if allearnings=="124,000"
replace allearnings="125000" if allearnings=="125,000"

replace allearnings="13000" if allearnings=="13,000"
replace allearnings="13500" if allearnings=="13,500"
replace allearnings="13600" if allearnings=="13,600"
replace allearnings="130000" if allearnings=="130,000"
replace allearnings="132000" if allearnings=="132,000"
replace allearnings="140000" if allearnings=="140,000"
replace allearnings="142000" if allearnings=="142,000"
replace allearnings="15000" if allearnings=="15,000"
replace allearnings="" if allearnings=="150,00" // unclear whether this should mean 150,000 or 15,000. Set to missing.
replace allearnings="150000" if allearnings=="150,000"

replace allearnings="15000" if allearnings=="15000."
replace allearnings="155000" if allearnings=="155,000"
replace allearnings="16000" if allearnings=="16,000"
replace allearnings="160000" if allearnings=="160,000"
replace allearnings="160000" if allearnings=="160,000.00"
replace allearnings="165000" if allearnings=="165,000"
replace allearnings="167000" if allearnings=="167,000"
replace allearnings="178000" if allearnings=="178,000"
replace allearnings="18000" if allearnings=="18,000"
replace allearnings="184500" if allearnings=="184,500.00"
replace allearnings="190000" if allearnings=="190,000.00"
replace allearnings="20000" if allearnings=="20,000"
replace allearnings="200000" if allearnings=="200,000"

replace allearnings="21000" if allearnings=="21,000"
replace allearnings="22000" if allearnings=="22,000"
replace allearnings="23688" if allearnings=="23,688"
replace allearnings="24000" if allearnings=="24,000"
replace allearnings="24592" if allearnings=="24,592.00"
replace allearnings="25000" if allearnings=="25,000"
replace allearnings="26000" if allearnings=="26,000" 
replace allearnings="27000" if allearnings=="27,000"
replace allearnings="27000" if allearnings=="27,000.00"
replace allearnings="27500" if allearnings=="27,500"
replace allearnings="28000" if allearnings=="28,000"

replace allearnings="3000" if allearnings=="3000."
replace allearnings="30000" if allearnings=="30,000"
replace allearnings="300000" if allearnings=="300,000"
replace allearnings="31000" if allearnings=="31,000"
replace allearnings="32000" if allearnings=="32,000"
replace allearnings="33000" if allearnings=="33,000.00"
replace allearnings="34000" if allearnings=="34,000"
replace allearnings="34500" if allearnings=="34,500"
replace allearnings="35000" if allearnings=="35,000"
replace allearnings="35000" if allearnings=="35,000.00"
replace allearnings="35000" if allearnings=="35000."
replace allearnings="37000" if allearnings=="37,000"

replace allearnings="37000" if allearnings=="37,000.00"
replace allearnings="38000" if allearnings=="38,000"
replace allearnings="380000" if allearnings=="380,000"
replace allearnings="39000" if allearnings=="39,000"
replace allearnings="4000" if allearnings=="4,000"
replace allearnings="40000" if allearnings=="40,000" 
replace allearnings="400000" if allearnings=="400,000" 
replace allearnings="40000" if allearnings=="40000."
replace allearnings="41000" if allearnings=="41,000"
replace allearnings="41000" if allearnings=="41,000.00"
replace allearnings="42000" if allearnings=="42,000.00"
replace allearnings="42300" if allearnings=="42,300"

replace allearnings="43000" if allearnings=="43,000"
replace allearnings="43500" if allearnings=="43,500"
replace allearnings="45000" if allearnings=="45,000"
replace allearnings="46000" if allearnings=="46,000"
replace allearnings="47000" if allearnings=="47,000"
replace allearnings="48000" if allearnings=="48,000"
replace allearnings="48000" if allearnings=="48,000.00"
replace allearnings="49000" if allearnings=="49,000.00"
replace allearnings="5000" if allearnings=="5,000"
replace allearnings="5000" if allearnings=="5,000.00"
replace allearnings="50000" if allearnings=="50,000"
replace allearnings="51000" if allearnings=="51,000"

replace allearnings="52000" if allearnings=="52,000"
replace allearnings="52500" if allearnings=="52,500"
replace allearnings="53000" if allearnings=="53,000"
replace allearnings="54000" if allearnings=="54,000"
replace allearnings="55000" if allearnings=="55,000"
replace allearnings="56150" if allearnings=="56,150"
replace allearnings="58000" if allearnings=="58,000"
replace allearnings="59000" if allearnings=="59,000"
replace allearnings="59425" if allearnings=="59,425"
replace allearnings="59980" if allearnings=="59,980"
replace allearnings="6000" if allearnings=="6,000"

replace allearnings="60000" if allearnings=="60,000" 
replace allearnings="65000" if allearnings=="65,000"
replace allearnings="66000" if allearnings=="66,000"
replace allearnings="68000" if allearnings=="68,000"
replace allearnings="7000" if allearnings=="7,000"
replace allearnings="70000" if allearnings=="70,000"
replace allearnings="72000" if allearnings=="72,000"
replace allearnings="75000" if allearnings=="75,000"
replace allearnings="79000" if allearnings=="79,000"

replace allearnings="8421" if allearnings=="8,421"
replace allearnings="80000" if allearnings=="80,000"
replace allearnings="81000" if allearnings=="81,000"
replace allearnings="82000" if allearnings=="82,000"
replace allearnings="83000" if allearnings=="83,000"
replace allearnings="85000" if allearnings=="85,000"
replace allearnings="90000" if allearnings=="90,000"
replace allearnings="93000" if allearnings=="93,000"
replace allearnings="96000" if allearnings=="96,000"
replace allearnings="96500" if allearnings=="96,500"

/* Re-coding those who indicated "Other industry" but should actually be classified as belonging to industry 1-8:
Source: https://www.osha.gov/pls/imis/sic_manual.html
1 - Construction
2 - Durable Manufacturing
3 - Finance, Insurance and Real Estate
4 - Health and Education
5 - Nondurable Manufacturing
6 - Retail and Wholesale
7 - Services
8 - Transportation, Communications and Other Public Utilities
9 - Other
*/

replace industry=7 if industry_text=="Animal rescue"
replace industry=6 if industry_text=="AUTOMOTIVE SALES"
replace industry=2 if industry_text=="Aerospace"
replace industry=2 if industry_text=="Aerospace & Defense"
replace industry=7 if industry_text=="Architecture"
replace industry=2 if industry_text=="BOLT PLANT FOR COAL MINES"
replace industry=6 if industry_text=="CAR DEALERSHIP"
replace industry=7 if industry_text=="Cleaning Service"
replace industry=7 if industry_text=="Clerical Manager"
replace industry=7 if industry_text=="Corporate travel"
replace industry=2 if industry_text=="Defense and Aerospace"
replace industry=4 if industry_text=="Education"
replace industry=8 if industry_text=="Electric and gas utility"
replace industry=7 if industry_text=="Engineering"
replace industry=7 if industry_text=="Engineering/scientific research"
replace industry=7 if industry_text=="Franchise managemetn"
replace industry=4 if industry_text=="Healthcare"
replace industry=6 if industry_text=="Import Export"
replace industry=7 if industry_text=="IT"|industry_text=="Information Technology"|industry_text=="information"|industry_text=="information Technology"|industry_text=="it"
replace industry=7 if industry_text=="Legal"
replace industry=7 if industry_text=="Leisure Entertainment"
replace industry=6 if industry_text=="Lubricant Distributor"
replace industry=5 if industry_text=="Media-Newspaper"
replace industry=7 if industry_text=="Musician"
replace industry=7 if industry_text=="Non-profit/social services"
replace industry=4 if industry_text=="Nurse"
replace industry=5 if industry_text=="Publishing"
replace industry=7 if industry_text=="Quality Consulting"
replace industry=3 if industry_text=="Real estate"
replace industry=7 if industry_text=="Refurbishment"
replace industry=7 if industry_text=="Religious Organization"
replace industry=6 if industry_text=="Sales"
replace industry=7 if industry_text=="Scientist"
replace industry=8 if industry_text=="Shipping"
replace industry=5 if industry_text=="Software development"
replace industry=8 if industry_text=="Storage units"
replace industry=8 if industry_text=="Telecommunications"
replace industry=7 if industry_text=="Travel Agent"
replace industry=7 if industry_text=="Travel and Tourism"
replace industry=8 if industry_text=="Twitch Streamer"
replace industry=8 if industry_text=="Utilities"
replace industry=5 if industry_text=="Veterinary"
replace industry=2 if industry_text=="aerospace"
replace industry=5 if industry_text=="agriculture"
replace industry=7 if industry_text=="architecture"
replace industry=6 if industry_text=="auto dealership"
replace industry=5 if industry_text=="biotech"
replace industry=7 if industry_text=="building maintence"
replace industry=7 if industry_text=="church"
replace industry=1 if industry_text=="civil engineering"
replace industry=7 if industry_text=="consulting"
replace industry=6 if industry_text=="distribution"
replace industry=4 if industry_text=="education"
replace industry=7 if industry_text=="engineering"
replace industry=7 if industry_text=="entertainment"
replace industry=7 if industry_text=="escort service owner"
replace industry=7 if industry_text=="fast food"
replace industry=7 if industry_text=="food service"
replace industry=7 if industry_text=="hospitality / food"
replace industry=7 if industry_text=="landscaper"
replace industry=7 if industry_text=="law"
replace industry=7 if industry_text=="legal"
replace industry=7 if industry_text=="library"
replace industry=7 if industry_text=="mechanic"
replace industry=4 if industry_text=="medical transcription"
replace industry=5 if industry_text=="newspaper"
replace industry=7 if industry_text=="private security"
replace industry=7 if industry_text=="professional/admin"
replace industry=5 if industry_text=="publishing"
replace industry=3 if industry_text=="real estate office"
replace industry=7 if industry_text=="religious"
replace industry=7 if industry_text=="restaraunt"
replace industry=7 if industry_text=="restaurant"
replace industry=7 if industry_text=="restuarant"
replace industry=4 if industry_text=="roi specialist"
replace industry=7 if industry_text=="science/research"
replace industry=6 if industry_text=="second hand store"
replace industry=2 if industry_text=="semiconductor"
replace industry=7 if industry_text=="taxes"
replace industry=8 if industry_text=="telecom"
replace industry=8 if industry_text=="truck driver"
replace industry=5 if industry_text=="veterinary services"

/* Correctly self-classified as "other industry":
Fire Service
Firefighter
forensics
GOVERMENT
Government
Govt
government
govt
Law Enforcement
law enforcement
Local Government
Local government
local goverment
local government
local law enforcement
Military
Public sector
State Public Services
State government
*/

* The following are unclear and set to missing:
replace industry=. if industry_text=="community support"
replace industry=. if industry_text=="human service"
replace industry=. if industry_text=="international relief work"
replace industry=. if industry_text=="licensing"
replace industry=. if industry_text=="lighting"

* Not possible to classify the following, mostly unclear whether production or distribution etc.
replace industry=. if industry_text=="8"
replace industry=. if industry_text=="automotive"
replace industry=. if industry_text=="Customer service"
replace industry=. if industry_text=="design"
replace industry=. if industry_text=="ed"
replace industry=. if industry_text=="Fine Wines"
replace industry=. if industry_text=="Food"
replace industry=. if industry_text=="food and beverage"
replace industry=. if industry_text=="I"
replace industry=. if industry_text=="Manufacturing"
replace industry=. if industry_text=="Medical Devices"
replace industry=. if industry_text=="Medical Software"
replace industry=. if industry_text=="Oil"
replace industry=. if industry_text=="Oil and Gas"
replace industry=. if industry_text=="Media"
replace industry=. if industry_text=="Non Profit"
replace industry=. if industry_text=="Non profit"
replace industry=. if industry_text=="Non-Profit"
replace industry=. if industry_text=="Non-profit"
replace industry=. if industry_text=="Nonprofit"
replace industry=. if industry_text=="non profit"
replace industry=. if industry_text=="non-profit"
replace industry=. if industry_text=="non-profit foundation"
replace industry=. if industry_text=="none"
replace industry=. if industry_text=="nonprofit"
replace industry=. if industry_text=="Pharmaceuticals"
replace industry=. if industry_text=="Semiconductor equipment"
replace industry=. if industry_text=="Software"
replace industry=. if industry_text=="software"
replace industry=. if industry_text=="software tech"
replace industry=. if industry_text=="tech"
replace industry=. if industry_text=="TRUCKS"
replace industry=. if industry_text=="Technology"
replace industry=. if industry_text=="transcription"
replace industry=. if industry_text=="travel"

foreach var in industry_text marstat_text pol_text q98 prior jobloss jobfind increaseunempl_prob increase_localunempl posterior{
replace `var'="" if `var'=="NA"
}

destring *, replace

gen t_start = d_start*24*60*60 + h_start*60*60 + m_start*60 + s_start
gen t_end = d_end*24*60*60 + h_end*60*60 + m_end*60 + s_end
gen duration = t_end - t_start
tab duration
la var duration "Duration in seconds"

keep newid ac duration prior_recession joblossprob_post increaseunempl_prob increase_localunempl jobfindingprob_post posterior_recession ///
 treatment q1 q146_1_text q146_2_text q146_3_text q146_4_text q146_5_text q146_6_text q146_7_text ///
q2 consent screener_1 screener_2 screener_3 screener_4 screener_5 male age region hhincome educ q28 q53 q32 conf ///
q57 v50 q55_1 q55_2 q55_3 q55_4 v55 q56_1 q56_2 q56_3 q56_4 unemployment v61 fin_prospects q29 incomeexpectations_4 incomeexpectations_6 incomeexpectations_7 incomeexpectations_8 ///
incomeexpectations_9 incomeexpectations_10 incomeexpectations_11 incomeexpectations_13 incomeexpectations_14 incomeexpectations_15 ///
q54 v75 inflationexpectation_4 inflationexpectation_6 inflationexpectation_8 inflationexpectation_9 inflationexpectation_10 ///
inflationexpectation_11 inflationexpectation_13 inflationexpectation_14 inflationexpectation_15 inflationexpectation_16 v86 q55 ///
behavioral food_1 food_2 food_4 profitability v95 v96 q51 q58 industry industry_text marstat marstat_text ///
earningspaidjobs allearnings publicprivate v106 v107 v108 fin_lit1 fin_lit2 fin_lit3 follownews_1 follownews_2 hhsize pol pol_text assets ///
zipcode yob q144 q98 locationlatitude locationlongitude locationaccuracy industry_text

merge 1:1 newid using "experimentRN1_IDs.dta" // merging new ID variable
drop _merge


***** Coding variables *****
****************************

gen attentive=0
replace attentive=1 if screener_1==1&screener_5==1
la var attentive "Attentive (passed screener)"

rename prior_recession prior
la var prior "Prior belief: Recession"

gen prior_conf=6-conf
la var prior_conf "Prior belief: Recession - Confidence"

rename posterior_recession posterior
la var posterior "Posterior belief: Recession"

replace treatment = treatment==1
la var treatment "Treatment: 35% forecast"

gen shock = 35-prior if treatment==1
replace shock = 5-prior if treatment==0
la var shock "Shock to beliefs"

gen unemp_cat=6-unemp
rename increaseunempl_prob unemp_prob

recode fin_prospects (8=2) (2=3) (3=4) (4=5) (7=6) (6=7), gen(finprospects)
drop fin_prospects
rename finprospects fin_prosp

gen earn_mean = (incomeexpectations_4*14 + incomeexpectations_6*10 + ///
incomeexpectations_7*6 + incomeexpectations_8*3 + incomeexpectations_9*1 - ///
incomeexpectations_10*1 - incomeexpectations_11*3 - incomeexpectations_13*6 - ///
incomeexpectations_14*10 - incomeexpectations_15*14)/100

gen earn_sd = sqrt((incomeexpectations_4*(14 - earn_mean)^2  + incomeexpectations_6*(10 - earn_mean)^2 + ///
incomeexpectations_7*(6 - earn_mean)^2 + incomeexpectations_8*(3 - earn_mean)^2 + incomeexpectations_9*(1 - earn_mean)^2 + ///
incomeexpectations_10*(-1 - earn_mean)^2 + incomeexpectations_11*(-3 - earn_mean)^2 + incomeexpectations_13*(-6 - earn_mean)^2 + ///
incomeexpectations_14*(-10 - earn_mean)^2 + incomeexpectations_15*(-14 - earn_mean)^2)/100)

gen earn_num_bins=0 if earn_mean!=.
foreach i in 4 6 7 8 9 10 11 13 14 15{
replace earn_num_bins=earn_num_bins+1 if incomeexpectations_`i'>0 & earn_mean!=.
}

gen earn_noncontiguous=0 if earn_mean!=.
forvalues i=1/2{
gen switch_pos_`i'=0 if earn_mean!=.
gen switch_neg_`i'=0 if earn_mean!=.
}
foreach i in 4 6 7 8 9 10 11 13 14 15{
replace switch_pos_1=`i' if switch_pos_1==0 & incomeexpectations_`i'>0
}
foreach i in 6 7 8 9 10 11 13 14 15{
replace switch_neg_1=`i' if switch_neg_1==0 & incomeexpectations_`i'==0 & switch_pos_1<`i'
}
foreach i in 7 8 9 10 11 13 14 15{
replace switch_pos_2=`i' if switch_pos_2==0 & incomeexpectations_`i'>0 & switch_pos_1<`i' & switch_neg_1<`i' & switch_pos_1!=0 & switch_neg_1!=0
}
replace earn_noncontiguous=1 if switch_pos_2>0 & earn_mean!=.
drop switch_*

rename incomeexpectations_4 earn_bin10
rename incomeexpectations_6 earn_bin9
rename incomeexpectations_7 earn_bin8
rename incomeexpectations_8 earn_bin7
rename incomeexpectations_9 earn_bin6
rename incomeexpectations_10 earn_bin5
rename incomeexpectations_11 earn_bin4
rename incomeexpectations_13 earn_bin3
rename incomeexpectations_14 earn_bin2
rename incomeexpectations_15 earn_bin1

rename joblossprob_post jobloss
rename jobfindingprob_post jobfind

gen jobinsecurity=(0.01*jobloss)*(0.01*(100-jobfind))*100

gen infl_mean = (inflationexpectation_4*14 + inflationexpectation_6*10 + ///
inflationexpectation_16*6 + inflationexpectation_8*3 + inflationexpectation_9*1 - ///
inflationexpectation_10*1 - inflationexpectation_11*3 - inflationexpectation_13*6 - ///
inflationexpectation_14*10 - inflationexpectation_15*14)/100

gen infl_sd = sqrt((inflationexpectation_4*(14 - infl_mean)^2 + inflationexpectation_6*(10 - infl_mean)^2 + ///
inflationexpectation_16*(6 - infl_mean)^2 + inflationexpectation_8*(3 - infl_mean)^2 + inflationexpectation_9*(1 - infl_mean)^2 + ///
inflationexpectation_10*(-1 - infl_mean)^2 + inflationexpectation_11*(-3 - infl_mean)^2 + inflationexpectation_13*(-6 - infl_mean)^2 + ///
inflationexpectation_14*(-10 - infl_mean)^2 + inflationexpectation_15*(-14 - infl_mean)^2)/100)

gen infl_num_bins=0 if infl_mean!=.
foreach i in 4 6 16 8 9 10 11 13 14 15{
replace infl_num_bins=infl_num_bins+1 if inflationexpectation_`i'>0 & infl_mean!=.
}

gen infl_noncontiguous=0 if infl_mean!=.
rename inflationexpectation_16 inflationexpectation_7
forvalues i=1/2{
gen switch_pos_`i'=0 if infl_mean!=.
gen switch_neg_`i'=0 if infl_mean!=.
}
foreach i in 4 6 7 8 9 10 11 13 14 15{
replace switch_pos_1=`i' if switch_pos_1==0 & inflationexpectation_`i'>0
}
foreach i in 6 7 8 9 10 11 13 14 15{
replace switch_neg_1=`i' if switch_neg_1==0 & inflationexpectation_`i'==0 & switch_pos_1<`i'
}
foreach i in 7 8 9 10 11 13 14 15{
replace switch_pos_2=`i' if switch_pos_2==0 & inflationexpectation_`i'>0 & switch_pos_1<`i' & switch_neg_1<`i' & switch_pos_1!=0 & switch_neg_1!=0
}
replace infl_noncontiguous=1 if switch_pos_2>0 & infl_mean!=.
drop switch_*

rename inflationexpectation_4 infl_bin10
rename inflationexpectation_6 infl_bin9
rename inflationexpectation_7 infl_bin8
rename inflationexpectation_8 infl_bin7
rename inflationexpectation_9 infl_bin6
rename inflationexpectation_10 infl_bin5
rename inflationexpectation_11 infl_bin4
rename inflationexpectation_13 infl_bin3
rename inflationexpectation_14 infl_bin2
rename inflationexpectation_15 infl_bin1

gen durables=9-behavioral

gen food_home=food_1-10
gen food_away=food_2-10
gen leisure=food_4-10

gen profits=8-profitability

rename increase_localunempl unemp_county_prob

gen spending=q58 if q58!=. & q58>=0
gen log_spending=log(q58+1)

la var unemp_cat "Higher unemployment: Categorical"
la var unemp_prob "Higher unemployment: Probability"
la var unemp_county_prob "Higher unemploymentin own county: Probability"

la var fin_prosp "Financial prospects: Categorical"
la var earn_mean "Earnings expectations: Mean"
la var earn_sd "Earnings expectations: Std. dev."
la var earn_num_bins "Earnings expectations: Number of bins with pos. prob."
la var earn_noncontiguous "Earnings expectations: Pos. prob. in non-contiguous bins"
la var jobloss "Job loss: Probability"
la var jobfind "Job finding: Probability"
la var jobinsecurity "Job insecurity (composite)"

la var infl_mean "Inflation expectations: Mean"
la var infl_sd "Inflation expectations: Std. dev."
la var infl_num_bins "Inflation expectations: Number of bins with pos. prob."
la var infl_noncontiguous "Inflation expectations: Pos. prob. in non-contiguous bins"

la var durables "Buying conditions: Durables"
la var food_home "Consumption plans: Food at home"
la var food_away "Consumption plans: Food away from home"
la var leisure "Consumption plans: Leisure activities"

la var spending "Non-durable spending last week"
la var log_spending "Log(Non-durable spending last week)"

la var profits "Higher profitability all firms: Categorical"

foreach var of varlist unemp_cat unemp_prob unemp_county_prob ///
fin_prosp earn_mean earn_sd jobloss jobfind jobinsecurity ///
infl_mean infl_sd ///
durables food_home food_away leisure ///
profits{
qui sum `var'
gen z_`var'=((`var'-r(mean))/r(sd))
}

gen ndcons_index_A=(1/3)*(z_food_home+z_food_away+z_leisure)
gen cons_index_A=(1/4)*(z_durables+z_food_home+z_food_away+z_leisure)

foreach var of varlist ndcons_index_A cons_index_A{
qui sum `var' 
gen z_`var'=((`var'-r(mean))/r(sd))
}


la var z_unemp_cat "Higher unemployment: Categorical (z-scored)"
la var z_unemp_prob "Higher unemployment: Probability (z-scored)"
la var z_unemp_county_prob "Higher unemploymentin own county: Probability (z-scored)"

la var z_fin_prosp "Financial prospects: Categorical (z-scored)"
la var z_earn_mean "Earnings expectations: Mean (z-scored)"
la var z_earn_sd "Earnings expectations: Std. dev. (z-scored)"
la var z_jobloss "Job loss: Probability (z-scored)"
la var z_jobfind "Job finding: Probability (z-scored)"
la var z_jobinsecurity "Job insecurity (composite)"

la var z_infl_mean "Inflation expectations: Mean (z-scored)"
la var z_infl_sd "Inflation expectations: Std. dev. (z-scored)"

la var z_durables "Buying conditions: Durables (z-scored)"
la var z_food_home "Consumption plans: Food at home (z-scored)"
la var z_food_away "Consumption plans: Food away from home (z-scored)"
la var z_leisure "Consumption plans: Leisure activities (z-scored)"
la var z_ndcons_index_A "Consumption plans: Index (unweighted)"
la var z_cons_index_A "Consumption nondurables and durables: Index (unweighted)"

la var z_profits "Higher profitability all firms: Categorical (z-scored)"

gen female = male==2
la var female "Female"

gen age_18_24=(age==1) if age!=.
la var age_18_24 "Age 18-24"
gen age_25_34=(age==2) if age!=.
la var age_25_34 "Age 25-34"
gen age_35_44=(age==3) if age!=.
la var age_35_44 "Age 35-44"
gen age_45_54=(age==4) if age!=.
la var age_45_54 "Age 45-54"
gen age_55_64=(age==5) if age!=.
la var age_55_64 "Age 55-64"
gen age_65=(age==6) if age!=.
la var age_65 "Age 65 and older"

gen age_check=age_18_24+age_25_34+age_35_44+age_45_54+age_55_64+age_65
tab age_check, m

gen northeast=(region==4) if region!=.
la var northeast "Northeast"
gen midwest=(region==6) if region!=.
la var midwest "Midwest"
gen south=(region==7) if region!=.
la var south "South"
gen west=(region==10) if region!=.
la var west "West"

gen region_check=northeast+midwest+south+west
tab region_check, m

gen inc_15000=(hhincome==1) if hhincome!=.
la var inc_15000 "Income < 15,000"
gen inc_15000_25000=(hhincome==4) if hhincome!=.
la var inc_15000_25000 "15,000 < Income < 25,000"
gen inc_25000_50000=(hhincome==6) if hhincome!=.
la var inc_25000_50000 "25,000 < Income < 50,000"
gen inc_50000_75000=(hhincome==3) if hhincome!=.
la var inc_50000_75000 "50,000 < Income < 75,000"
gen inc_75000_100000=(hhincome==2) if hhincome!=.
la var inc_75000_100000 "75,000 < Income < 100,000"
gen inc_100000_150000=(hhincome==9) if hhincome!=.
la var inc_100000_150000 "100,000 < Income < 150,000"
gen inc_150000_200000=(hhincome==11) if hhincome!=.
la var inc_150000_200000 "150,000 < Income < 200,000"
gen inc_200000=(hhincome==8) if hhincome!=.
la var inc_200000 "Income > 200,000"
gen inc_NR=(hhincome==7)
la var inc_NR "Income: Prefer not to say"

gen income_check=inc_15000+inc_15000_25000+inc_25000_50000+inc_50000_75000+inc_75000_100000+inc_100000_150000+inc_150000_200000+inc_200000+inc_NR
tab income_check, m

recode hhincome (1=7500) (4=20000) (6=37500) (3=62500) (2=87500) (9=125000) (11=175000) (8=250000) (7=.), gen(income)
la var income "Income"

gen log_income=log(income)
la var log_income "Log(Income)"

gen below_highschool=(educ==1) if educ!=.
la var below_highschool "Below highschool"
gen highschool=(educ==2) if educ!=.
la var highschool "Highschool"
gen some_college=(educ==3) if educ!=.
la var some_college "Some college"
gen associate_degree=(educ==4) if educ!=.
la var associate_degree "Associate degree"
gen bachelors_degree=(educ==5) if educ!=.
la var bachelors_degree "Bachelor's degree"
gen postgrad_degree=(educ==6) if educ!=.
la var postgrad_degree "Post-graduate degree"

gen educ_check=below_highschool+highschool+some_college+associate_degree+bachelors_degree+postgrad_degree
tab educ_check, m

gen atleast_bachelor=(educ>=5) if educ!=.
la var atleast_bachelor "At least bachelor's degree"

la var industry "Industry"

gen construction=(industry==1) if industry!=.
la var construction "Industry: Construction"
gen manuf_durable=(industry==2) if industry!=.
la var manuf_durable "Industry: Durable manufacturing"
gen finance=(industry==3) if industry!=.
la var finance "Industry: Finance"
gen health_educ=(industry==4) if industry!=.
la var health_educ "Industry: Health and education"
gen manuf_nondurable=(industry==5) if industry!=.
la var manuf_nondurable "Industry: Nondurable manufacturing"
gen retail=(industry==6) if industry!=.
la var retail "Industry: Retail and wholesale"
gen services=(industry==7) if industry!=.
la var services "Industry: Services"
gen transportation=(industry==8) if industry!=.
la var transportation "Industry: Transportation"
gen ind_other=(industry==9) if industry!=.
la var ind_other "Industry: Other"

gen ind_check=construction+manuf_durable+finance+health_educ+manuf_nondurable+retail+services+transportation+ind_other
tab ind_check, m

la var industry_text "Industry (Text entry)"

gen single=(marstat==1) if marstat!=.
la var single "Single"
gen married=(marstat==2) if marstat!=.
la var married "Married"
gen separated=(marstat==3) if marstat!=.
la var separated "Separated"
gen divorced=(marstat==4) if marstat!=.
la var divorced "Divorced"
gen widowed=(marstat==5) if marstat!=.
la var widowed "Widowed"
gen marstat_other=(marstat==6) if marstat!=.
la var marstat_other "Marital status: Other"

gen marstat_check=single+married+separated+divorced+widowed+marstat_other
tab marstat_check, m

gen earn_2016_wtr=(earningspaidjobs==2) if earningspaidjobs==2|earningspaidjobs==4
la var earn_2016_wtr "Earnings from paid jobs in 2016: Whether"
gen earn_2016_NR=(earningspaidjobs==3) if earningspaidjobs!=.
la var earn_2016_NR "Earnings from paid jobs in 2016: NR"
rename allearnings earn_2016_level
la var earn_2016_level "Earnings from paid jobs in 2016: Level"

gen log_earn_2016_level=log(earn_2016_level) if earn_2016_level!=.&earn_2016_level>0
la var log_earn_2016_level "Log(Earnings from paid jobs in 2016)"

gen public_employer=(publicprivate==1) if publicprivate!=.
la var public_employer "Public employer"
gen private_employer=(publicprivate==2) if publicprivate!=.
la var private_employer "Private employer"
gen other_employer=(publicprivate==3) if publicprivate!=.
la var other_employer "Other employer"

gen employer_check=public_employer+private_employer+other_employer
tab employer_check, m

gen tenure_1=(v106==1) if v106!=.
la var tenure_1 "Tenure: Less than 1 year"
gen tenure_1_2=(v106==3) if v106!=.
la var tenure_1_2 "Tenure: 1-2 years"
gen tenure_2_3=(v106==4) if v106!=.
la var tenure_2_3 "Tenure: 2-3 years"
gen tenure_3_4=(v106==5) if v106!=.
la var tenure_3_4 "Tenure: 3-4 years"
gen tenure_4_5=(v106==6) if v106!=.
la var tenure_4_5 "Tenure: 4-5 years"
gen tenure_5_10=(v106==8) if v106!=.
la var tenure_5_10 "Tenure: 5-10 years"
gen tenure_10_20=(v106==9) if v106!=.
la var tenure_10_20 "Tenure: 10-20 years"
gen tenure_20=(v106==10) if v106!=.
la var tenure_20 "Tenure: More than 20 years"

gen tenure_check=tenure_1+tenure_1_2+tenure_2_3+tenure_3_4+tenure_4_5+tenure_5_10+tenure_10_20+tenure_20
tab tenure_check, m

gen tenure=0.5 if tenure_1==1
replace tenure=1.5 if tenure_1_2==1
replace tenure=2.5 if tenure_2_3==1
replace tenure=3.5 if tenure_3_4==1
replace tenure=4.5 if tenure_4_5==1
replace tenure=7.5 if tenure_5_10==1
replace tenure=15 if tenure_10_20==1
replace tenure=25 if tenure_20==1
la var tenure "Tenure"

gen hours_10=(v107==1) if v107!=.
la var hours_10 "Weekly hours worked: Less than 10"
gen hours_10_20=(v107==3) if v107!=.
la var hours_10_20 "Weekly hours worked: 10 to 20"
gen hours_20_30=(v107==4) if v107!=.
la var hours_20_30 "Weekly hours worked: 20 to 30"
gen hours_30_40=(v107==5) if v107!=.
la var hours_30_40 "Weekly hours worked: 30 to 40"
gen hours_40_50=(v107==6) if v107!=.
la var hours_40_50 "Weekly hours worked: 40 to 50"
gen hours_50_60=(v107==8) if v107!=.
la var hours_50_60 "Weekly hours worked: 50 to 60"
gen hours_60=(v107==9) if v107!=.
la var hours_60 "Weekly hours worked: More than 60"

gen hours_check=hours_10+hours_10_20+hours_20_30+hours_30_40+hours_40_50+hours_50_60+hours_60
tab hours_check, m

gen hours=5 if hours_10==1
replace hours=15 if hours_10_20==1
replace hours=25 if hours_20_30==1
replace hours=35 if hours_30_40==1
replace hours=45 if hours_40_50==1
replace hours=55 if hours_50_60==1
replace hours=65 if hours_60==1
la var hours "Weekly hours worked"

gen finlit_int=(fin_lit1==1) if fin_lit1!=.
la var finlit_int "Fin. literacy: Interest compounding - Correct"
gen finlit_infl=(fin_lit2==3) if fin_lit2!=.
la var finlit_infl "Fin. literacy: Inflation - Correct"
gen finlit_div=(fin_lit3==2) if fin_lit3!=.
la var finlit_div "Fin. literacy: Diversification - Correct"

gen finlit_index=finlit_int+finlit_infl+finlit_div
la var finlit_index "Fin. literacy index"

gen follow_news=follownews_1-17
la var follow_news "Follow news about economy: Categorical"
gen unconstrained=follownews_2-17
la var unconstrained "Can easily borrow money: Categorical"

gen famsize_1=(hhsize<=2) if hhsize!=.
la var famsize_1 "Family members: 1"
gen famsize_2=(hhsize==3) if hhsize!=.
la var famsize_2 "Family members: 2"
gen famsize_3=(hhsize==4) if hhsize!=.
la var famsize_3 "Family members: 3"
gen famsize_4=(hhsize==5) if hhsize!=.
la var famsize_4 "Family members: 4"
gen famsize_5=(hhsize>=6) if hhsize!=.
la var famsize_5 "Family members: 5 or more"

gen famsize_check=famsize_1+famsize_2+famsize_3+famsize_4+famsize_5
tab famsize_check, m

replace hhsize=hhsize-1

rename hhsize famsize
la var famsize "Familiy size"

gen republican=(pol==1) if pol!=.
la var republican "Republican"
gen democrat=(pol==2) if pol!=.
la var democrat "Democrat"
gen independent=(pol==3) if pol!=.
la var independent "Independent"

gen pol_check=republican+democrat+independent
tab pol_check, m

gen assets_500=(assets==1) if assets!=.
la var assets_500 "Liquid assets < 500"
gen assets_500_1000=(assets==2) if assets!=.
la var assets_500_1000 "500 < Liquid assets < 1,000"
gen assets_1000_2000=(assets==3) if assets!=.
la var assets_1000_2000 "1,000 < Liquid assets < 2,000"
gen assets_2000_5000=(assets==4) if assets!=.
la var assets_2000_5000 "2,000 < Liquid assets < 5,000"
gen assets_5000_20000=(assets==5) if assets!=.
la var assets_5000_20000 "5,000 < Liquid assets < 20,000"
gen assets_20000_50000=(assets==6) if assets!=.
la var assets_20000_50000 "20,000 < Liquid assets < 50,000"
gen assets_50000_200000=(assets==7) if assets!=.
la var assets_50000_200000 "50,000 < Liquid assets < 200,000"
gen assets_200000=(assets==8) if assets!=.
la var assets_200000 "Liquid assets > 200,000"
gen assets_NR=(assets==9) if assets!=.
la var assets_NR "Liquid assets: Prefer not to say "

gen assets_check=assets_500+assets_500_1000+assets_1000_2000+assets_2000_5000+assets_5000_20000+assets_20000_50000+assets_50000_200000+assets_200000+assets_NR
tab assets_check, m

recode assets (1=250) (2=750) (3=1500) (4=2500) (5=12500) (6=35000) (7=125000) (8=250000) (9=.), gen(assets_level)
replace assets=assets_level
drop assets_level
la var assets "Liquid assets"

gen log_assets=log(assets)
la var log_assets "Log(Liquid assets)"

la var zipcode "Zip code"

gen birth_year=2000-yob if yob!=.
la var birth_year "Year of birth"

drop age
gen age=2017-birth_year
la var age "Age"

gen agesq=age^2
la var agesq "Age squared"

drop if ac==.



***** Generating additional variables *****
*******************************************

***** Merging zip code-county crosswalk *****
cd "$path_data_raw"
merge m:1 zipcode using "zip_county_crosswalk.dta"
drop if _merge==2 // dropping zip codes that do not appear in the Master data.
drop _merge

***** Mergeing county-level unemployment data *****
cd "$path_data_raw"
merge m:1 geofips using "unemprates_countylevel.dta", keepusing(unemp_rate2014 unemp_rate2015 unemp_rate2016 unemp_rate_incr_1y unemp_rate_incr_2y unemp_rate_incr_3y unemp_rate_incr_5y unemp_rate_incr_10y unemp_rate_incr_15y)
drop if _merge==2
drop _merge

gen unemp_rate3y=(1/3)*(unemp_rate2014+unemp_rate2015+unemp_rate2016)
la var unemp_rate3y "Unemployment rate (county-level) avg. last 3 yrs."

la var unemp_rate2016 "Unemployment rate (county-level) in 2016"
la var unemp_rate2014 "Unemployment rate (county-level) in 2015"
la var unemp_rate2015 "Unemployment rate (county-level) in 2014"

gen mis_age=(age==.)

replace age=21 if age_18_24==1 & age==.
replace age=29 if age_25_34==1 & age==.
replace age=39 if age_35_44==1 & age==.
replace age=49 if age_45_54==1 & age==.
replace age=59 if age_55_64==1 & age==.
replace age=69 if age_65==1 & age==.

replace birth_year=2017-age if birth_year==.

replace agesq=age^2 if agesq==.

gen timeontreatmentpage = q55_3
replace timeontreatmentpage = q56_3 if timeontreatmentpage==.

tab timeontreatmentpage


***** Cleaning up *****
***********************

global vars ///
newid rnid duration attentive prior prior_conf posterior timeontreatmentpage ///
treatment shock ///
unemp_cat unemp_prob unemp_county_prob ///
fin_prosp earn_mean earn_sd earn_num_bins earn_noncontiguous jobloss jobfind jobinsecurity ///
infl_mean infl_sd infl_num_bins infl_noncontiguous ///
durables food_home food_away leisure spending log_spending ///
profits ///
z_unemp_cat z_unemp_prob z_unemp_county_prob ///
z_fin_prosp z_earn_mean z_earn_sd z_jobloss z_jobfind z_jobinsecurity ///
z_infl_mean z_infl_sd  ///
z_durables z_food_home z_food_away z_leisure z_cons_index_A z_ndcons_index_A ///
z_profits ///
geofips northeast midwest south west state statefips countyfips zipcode ///
female birth_year age agesq age_18_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65 ///
below_highschool highschool some_college associate_degree bachelors_degree postgrad_degree atleast_bachelor ///
industry industry_text construction manuf_durable finance health_educ manuf_nondurable retail services transportation ind_other ///
earn_2016_wtr earn_2016_NR earn_2016_level log_earn_2016_level ///
public_employer private_employer other_employer ///
tenure tenure_1 tenure_1_2 tenure_2_3 tenure_3_4 tenure_4_5 tenure_5_10 tenure_10_20 tenure_20 ///
hours hours_10 hours_10_20 hours_20_30 hours_30_40 hours_40_50 hours_50_60 hours_60 ///
single married separated divorced widowed marstat_other ///
finlit_int finlit_infl finlit_div finlit_index ///
follow_news unconstrained ///
republican democrat independent ///
famsize famsize_1 famsize_2 famsize_3 famsize_4 famsize_5 ///
income log_income inc_15000 inc_15000_25000 inc_25000_50000 inc_50000_75000 inc_75000_100000 inc_100000_150000 inc_150000_200000 inc_200000 inc_NR ///
assets log_assets assets_500 assets_500_1000 assets_1000_2000 assets_2000_5000 assets_5000_20000 assets_20000_50000 assets_50000_200000 assets_200000 assets_NR ///
unemp_rate2014 unemp_rate2015 unemp_rate2016 unemp_rate3y unemp_rate_incr_1y unemp_rate_incr_2y unemp_rate_incr_3y unemp_rate_incr_5y unemp_rate_incr_10y unemp_rate_incr_15y ///
infl_bin1 infl_bin2 infl_bin3 infl_bin4 infl_bin5 infl_bin6 infl_bin7 infl_bin8 infl_bin9 infl_bin10 ///
earn_bin1 earn_bin2 earn_bin3 earn_bin4 earn_bin5 earn_bin6 earn_bin7 earn_bin8 earn_bin9 earn_bin10

keep $vars
order $vars

drop if prior==.

cd "$path_data_processed"
save "experimentRN1.dta", replace







************************************************
***** 2) Main experiment: Follow-up survey *****
************************************************

cd "$path_data_raw"

clear
insheet using "experimentRN2.csv", names

rename zipcode zipcode_f

rename v10 finished
la var finished "Finished"

gen d_start = substr(v8,9,2)
tab d_start
gen h_start = substr(v8,12,2)
tab h_start
gen m_start = substr(v8,15,2)
tab m_start
gen s_start = substr(v8,18,2)
tab s_start
gen d_end = substr(v9,9,2)
tab d_end
gen h_end = substr(v9,12,2)
tab h_end
gen m_end = substr(v9,15,2)
tab m_end
gen s_end = substr(v9,18,2)
tab s_end

replace currentearnings="1200" if currentearnings=="1,200.00"
replace currentearnings="1600" if currentearnings=="1,600"
replace currentearnings="10000" if currentearnings=="10,000"
replace currentearnings="100000" if currentearnings=="100,000"
replace currentearnings="100000" if currentearnings=="100,000.00"
replace currentearnings="10000" if currentearnings=="10000."
replace currentearnings="106700" if currentearnings=="106,700"
replace currentearnings="11000" if currentearnings=="11,000"
replace currentearnings="11000" if currentearnings=="11,000.00"
replace currentearnings="12199" if currentearnings=="12,199"

replace currentearnings="12500" if currentearnings=="12,500"
replace currentearnings="123500" if currentearnings=="123,500"
replace currentearnings="126000" if currentearnings=="126,000.00"
replace currentearnings="128000" if currentearnings=="128,000.00"
replace currentearnings="14500" if currentearnings=="14,500"
replace currentearnings="15000" if currentearnings=="15,000"
replace currentearnings="16000" if currentearnings=="16,000"
replace currentearnings="17000" if currentearnings=="17,000"
replace currentearnings="18000" if currentearnings=="18,000"
replace currentearnings="2800" if currentearnings=="2,800"

replace currentearnings="20000" if currentearnings=="20,000"
replace currentearnings="200000" if currentearnings=="200,000"
replace currentearnings="21000" if currentearnings=="21,000"
replace currentearnings="24000" if currentearnings=="24,000"
replace currentearnings="25000" if currentearnings=="25,000"
replace currentearnings="250000" if currentearnings=="250,000"
replace currentearnings="28000" if currentearnings=="28,000"
replace currentearnings="3250" if currentearnings=="3,250"
replace currentearnings="3978" if currentearnings=="3,978"
replace currentearnings="30000" if currentearnings=="30,000"

replace currentearnings="32440" if currentearnings=="32,440"
replace currentearnings="33333" if currentearnings=="33,333.00"
replace currentearnings="34000" if currentearnings=="34,000"
replace currentearnings="4000" if currentearnings=="4,000"
replace currentearnings="40000" if currentearnings=="40,000"
replace currentearnings="43000" if currentearnings=="43,000"
replace currentearnings="44000" if currentearnings=="44,000"
replace currentearnings="45000" if currentearnings=="45,000"
replace currentearnings="46500" if currentearnings=="46,500"
replace currentearnings="5200" if currentearnings=="5,200.00"

replace currentearnings="50000" if currentearnings=="50,000"
replace currentearnings="52000" if currentearnings=="52,000"
replace currentearnings="55804.87" if currentearnings=="55,804.87"
replace currentearnings="56000" if currentearnings=="56,000"
replace currentearnings="58000" if currentearnings=="58,000"
replace currentearnings="59000" if currentearnings=="59,000"
replace currentearnings="6000" if currentearnings=="6,000"
replace currentearnings="6833" if currentearnings=="6,833"
replace currentearnings="62000" if currentearnings=="62,000"
replace currentearnings="63500" if currentearnings=="63,500"

replace currentearnings="65000" if currentearnings=="65,000"
replace currentearnings="7000" if currentearnings=="7000."
replace currentearnings="72000" if currentearnings=="72,000"
replace currentearnings="75000" if currentearnings=="75,000"
replace currentearnings="79000" if currentearnings=="79,000"
replace currentearnings="8000" if currentearnings=="8,000"
replace currentearnings="8333" if currentearnings=="8,333"
replace currentearnings="8875" if currentearnings=="8,875"
replace currentearnings="80000" if currentearnings=="80,000"
replace currentearnings="85000" if currentearnings=="85,000"
replace currentearnings="9000" if currentearnings=="9,000"

tab currentearnings, m

/* Coding industry classification.
Source: https://www.osha.gov/pls/imis/sic_manual.html
1 - Construction
2 - Durable Manufacturing
3 - Finance, Insurance and Real Estate
4 - Health and Education
5 - Nondurable Manufacturing
6 - Retail and Wholesale
7 - Services
8 - Transportation, Communications and Other Public Utilities
9 - Other
*/

rename industry industry_text_f
gen industry_f=""
replace industry_f="6" if industry_text_f=="AUTOMOBILE SALES"
replace industry_f="2" if industry_text_f=="Aerospace Engineering"
replace industry_f="2" if industry_text_f=="Aerospace"
replace industry_f="8" if industry_text_f=="Airline transportation"
replace industry_f="7" if industry_text_f=="Appliance Repair"
replace industry_f="7" if industry_text_f=="Architecture/Design"
replace industry_f="7" if industry_text_f=="Association Mamagement"
replace industry_f="2" if industry_text_f=="Automotive Parts Manufacturing"
replace industry_f="3" if industry_text_f=="Banking"
replace industry_f="7" if industry_text_f=="Business Services"
replace industry_f="7" if industry_text_f=="Business services"
replace industry_f="4" if industry_text_f=="CNA"
replace industry_f="1" if industry_text_f=="CONSTRUCTION"
replace industry_f="5" if industry_text_f=="CPG Manufacturing"
replace industry_f="2" if industry_text_f=="Car Manufacturing"
replace industry_f="5" if industry_text_f=="Chemical manufacturing"
replace industry_f="5" if industry_text_f=="Chemicals Manufacturing"
replace industry_f="1" if industry_text_f=="Civil Engineering"
replace industry_f="9" if industry_text_f=="Civil service"
replace industry_f="7" if industry_text_f=="Clerical Management"
replace industry_f="7" if industry_text_f=="Commercial Cleaning"
replace industry_f="5" if industry_text_f=="Commercial Printing / Graphic Design"
replace industry_f="8" if industry_text_f=="Communications"
replace industry_f="1" if industry_text_f=="Construction"
replace industry_f="7" if industry_text_f=="Consulitng"
replace industry_f="7" if industry_text_f=="Consulting"
replace industry_f="6" if industry_text_f=="Convenience store"
replace industry_f="9" if industry_text_f=="Corrections"
replace industry_f="9" if industry_text_f=="DOD"
replace industry_f="6" if industry_text_f=="Distribution"
replace industry_f="4" if industry_text_f=="EDUCATION"
replace industry_f="4" if industry_text_f=="Education"
replace industry_f="4" if industry_text_f=="Education (Non-profit)"
replace industry_f="7" if industry_text_f=="Engineering"
replace industry_f="7" if industry_text_f=="Engineering services"
replace industry_f="7" if industry_text_f=="Entertainment/Lesiure/Recreation"
replace industry_f="3" if industry_text_f=="FINANCIAL SERVICES"
replace industry_f="6" if industry_text_f=="FURNITURE RETAIL"
replace industry_f="3" if industry_text_f=="Finance"|industry_text_f=="Financial"|industry_text_f=="Financial Services"|industry_text_f=="Financial Services (Accounting)"
replace industry_f="7" if industry_text_f=="Food Service"
replace industry_f="5" if industry_text_f=="Food manufacturing"
replace industry_f="8" if industry_text_f=="Freight Transportation"
replace industry_f="9" if industry_text_f=="GOVERMENT"|industry_text_f=="Government"|industry_text_f=="Government ran law enforcemrnt"|industry_text_f=="Government/public sector"|industry_text_f=="Goverrnment"
replace industry_f="4" if industry_text_f=="HEALTHCARE"|industry_text_f=="Healt Services"|industry_text_f=="Health"|industry_text_f=="Health Care"|industry_text_f=="Health Services"|industry_text_f=="Health care services"|industry_text_f=="Health services"|industry_text_f=="Healthcare"
replace industry_f="4" if industry_text_f=="HIGHER EDUCATION"
replace industry_f="4" if industry_text_f=="Higher Education"
replace industry_f="7" if industry_text_f=="Hospitality"
replace industry_f="2" if industry_text_f=="Hot water heater manufacturing"
replace industry_f="7" if industry_text_f=="IT Consulting"
replace industry_f="6" if industry_text_f=="IT security sales"
replace industry_f="7" if industry_text_f=="Information Services"|industry_text_f=="IT"|industry_text_f=="Information"|industry_text_f=="Information Technology"
replace industry_f="3" if industry_text_f=="Insurance"
replace industry_f="3" if industry_text_f=="Insurance/Government Services"
replace industry_f="7" if industry_text_f=="Internet Security"
replace industry_f="3" if industry_text_f=="Investment Management"
replace industry_f="5" if industry_text_f=="Journalism"
replace industry_f="9" if industry_text_f=="LOCAL GOVERNMENT"|industry_text_f=="Local Government"
replace industry_f="8" if industry_text_f=="LOGISTICS"|industry_text_f=="Logistics"
replace industry_f="7" if industry_text_f=="Legal"|industry_text_f=="Legal services"
replace industry_f="7" if industry_text_f=="Librarian"
replace industry_f="2" if industry_text_f=="Manufacturing....Auto supplier weathe.."
replace industry_f="5" if industry_text_f=="Media - Newspaper"
replace industry_f="4" if industry_text_f=="Medical"|industry_text_f=="Mental Health Services"
replace industry_f="2" if industry_text_f=="Metal fabrication"
replace industry_f="9" if industry_text_f=="Military"
replace industry_f="7" if industry_text_f=="Non-profit Religious"
replace industry_f="7" if industry_text_f=="Nonprofit Museum"
replace industry_f="4" if industry_text_f=="North Carolina Central University"
replace industry_f="6" if industry_text_f=="Pro Sports Team -Sales"
replace industry_f="7" if industry_text_f=="Professional & Engineering Services"|industry_text_f=="Professional Services"|industry_text_f=="Professional Services - accounting"|industry_text_f=="Property Management"
replace industry_f="9" if industry_text_f=="Public Administration"
replace industry_f="4" if industry_text_f=="Public Sector-Education"
replace industry_f="5" if industry_text_f=="Publishing"
replace industry_f="3" if industry_text_f=="Real Estate"|industry_text_f=="Real estate"
replace industry_f="7" if industry_text_f=="Religous Ministry"
replace industry_f="7" if industry_text_f=="Research/Scientific"
replace industry_f="7" if industry_text_f=="Restaurant"|industry_text_f=="Restaurants"
replace industry_f="6" if industry_text_f=="Retail"|industry_text_f=="Retail Apparel and Footwear"
replace industry_f="6" if industry_text_f=="Retail Auto Sales"
replace industry_f="7" if industry_text_f=="Science"|industry_text_f=="Sciences"
replace industry_f="7" if industry_text_f=="Services"
replace industry_f="8" if industry_text_f=="Shipping/Distribution"
replace industry_f="7" if industry_text_f=="Social services"
replace industry_f="7" if industry_text_f=="Software Development"|industry_text_f=="Software development"|industry_text_f=="Software"
replace industry_f="5" if industry_text_f=="Specialty Chemical Manufacturing in t.."
replace industry_f="9" if industry_text_f=="State"|industry_text_f=="State Goverment - Education"|industry_text_f=="State Government"|industry_text_f=="State Government - Operations"|industry_text_f=="State government."
replace industry_f="2" if industry_text_f=="Steel Supply"|industry_text_f=="Steelmaking"
replace industry_f="8" if industry_text_f=="TRANSPORTATION"|industry_text_f=="Telecommunications"|industry_text_f=="Transportation"|industry_text_f=="USPS"|industry_text_f=="Utilities"|industry_text_f=="Warehousing/Logistics"
replace industry_f="7" if industry_text_f=="Theme Park"
replace industry_f="7" if industry_text_f=="Travel"|industry_text_f=="Travel & Hospitality"
replace industry_f="9" if industry_text_f=="US Government"
replace industry_f="4" if industry_text_f=="University Education"|industry_text_f=="University education"|industry_text_f=="University-level education"
replace industry_f="5" if industry_text_f=="Veterinary"
replace industry_f="7" if industry_text_f=="WAITRESS"
replace industry_f="6" if industry_text_f=="Wholesale"|industry_text_f=="Wholesale goods"
replace industry_f="7" if industry_text_f=="advertising"
replace industry_f="2" if industry_text_f=="aerospace"|industry_text_f=="aerospace and defense"|industry_text_f=="aerospace/defense aviation"
replace industry_f="5" if industry_text_f=="agriculture"
replace industry_f="7" if industry_text_f=="architecture"
replace industry_f="6" if industry_text_f=="automotive dealership"|industry_text_f=="car sales"
replace industry_f="6" if industry_text_f=="automotive parts-wholesale"
replace industry_f="3" if industry_text_f=="bank"|industry_text_f=="banking"
replace industry_f="7" if industry_text_f=="business communication services"
replace industry_f="2" if industry_text_f=="car manufacturing"
replace industry_f="7" if industry_text_f=="chemical management services"
replace industry_f="7" if industry_text_f=="church"|industry_text_f=="clerical"
replace industry_f="8" if industry_text_f=="commercial airline transportation"
replace industry_f="7" if industry_text_f=="computer software"
replace industry_f="1" if industry_text_f=="construction"
replace industry_f="7" if industry_text_f=="consulting"
replace industry_f="1" if industry_text_f=="contruction industies"
replace industry_f="2" if industry_text_f=="defense"
replace industry_f="4" if industry_text_f=="dental"
replace industry_f="6" if industry_text_f=="distribution"|industry_text_f=="e-commerce"
replace industry_f="7" if industry_text_f=="dog handler"
replace industry_f="2" if industry_text_f=="durable goods manufacturer"
replace industry_f="4" if industry_text_f=="education"|industry_text_f=="education - teacher"
replace industry_f="8" if industry_text_f=="electric utility"
replace industry_f="7" if industry_text_f=="engineer"|industry_text_f=="engineering"|industry_text_f=="engineering design"
replace industry_f="8" if industry_text_f=="entertainment media"
replace industry_f="4" if industry_text_f=="envelope manufacturing"
replace industry_f="7" if industry_text_f=="fast food"
replace industry_f="9" if industry_text_f=="federal government"
replace industry_f="3" if industry_text_f=="finance"|industry_text_f=="financial"|industry_text_f=="financial services"
replace industry_f="7" if industry_text_f=="financial software"|industry_text_f=="flooring"
replace industry_f="5" if industry_text_f=="food manufacturing"
replace industry_f="5" if industry_text_f=="food service"|industry_text_f=="food services"
replace industry_f="9" if industry_text_f=="government"|industry_text_f=="government storekeeper vehicle mainte.."
replace industry_f="6" if industry_text_f=="grocery"
replace industry_f="3" if industry_text_f=="health insurance"
replace industry_f="4" if industry_text_f=="health"|industry_text_f=="health care"|industry_text_f=="health field"|industry_text_f=="health services"|industry_text_f=="healthcare"|industry_text_f=="higher education"|industry_text_f=="hospital"
replace industry_f="7" if industry_text_f=="hospitality"|industry_text_f=="hospitlity"
replace industry_f="7" if industry_text_f=="graphic arts"
replace industry_f="7" if industry_text_f=="information"|industry_text_f=="information technology"|industry_text_f=="internet technology"|industry_text_f=="it manager"|industry_text_f=="janitor"|industry_text_f=="janitorial"
replace industry_f="3" if industry_text_f=="insurance"|industry_text_f=="insurance sales"
replace industry_f="7" if industry_text_f=="landscape"|industry_text_f=="landscape construction"
replace industry_f="9" if industry_text_f=="law enforcement"|industry_text_f=="law enforcemnt"|industry_text_f=="law enforcment local government"
replace industry_f="7" if industry_text_f=="law  law firm"|industry_text_f=="legal"|industry_text_f=="legal servics"|industry_text_f=="librarian"|industry_text_f=="library"
replace industry_f="8" if industry_text_f=="logistics"|industry_text_f=="ocean shipping"
replace industry_f="7" if industry_text_f=="maintence"|industry_text_f=="management consulting"
replace industry_f="2" if industry_text_f=="manufacturing motor parts"
replace industry_f="7" if industry_text_f=="musuem"
replace industry_f="6" if industry_text_f=="online sales"
replace industry_f="5" if industry_text_f=="petrochemical industry"|industry_text_f=="printing"|industry_text_f=="publishing"
replace industry_f="7" if industry_text_f=="professional services"|industry_text_f=="property manager"|industry_text_f=="public library"
replace industry_f="9" if industry_text_f=="public sector"
replace industry_f="7" if industry_text_f=="personal services"
replace industry_f="8" if industry_text_f=="public transit"
replace industry_f="3" if industry_text_f=="real estate"|industry_text_f=="real estste sales"
replace industry_f="7" if industry_text_f=="research"
replace industry_f="6" if industry_text_f=="retail"|industry_text_f=="retail (eCommerce)"|industry_text_f=="retail goods"|industry_text_f=="retail home improvement"|industry_text_f=="retail sales"
replace industry_f="4" if industry_text_f=="roi specialist"
replace industry_f="7" if industry_text_f=="retail store auditor"
replace industry_f="2" if industry_text_f=="roof bolt plant for the COAL MINES"
replace industry_f="7" if industry_text_f=="science"|industry_text_f=="security"
replace industry_f="2" if industry_text_f=="semiconductor"|industry_text_f=="semiconductor equipment"|industry_text_f=="semiconductor equipment manufacturing"
replace industry_f="7" if industry_text_f=="service"|industry_text_f=="services"
replace industry_f="3" if industry_text_f=="small lender"
replace industry_f="7" if industry_text_f=="social services"|industry_text_f=="software"
replace industry_f="5" if industry_text_f=="specialty chemical manufacturing"
replace industry_f="9" if industry_text_f=="state government"
replace industry_f="7" if industry_text_f=="tax manager"
replace industry_f="8" if industry_text_f=="telecommunication"|industry_text_f=="telecommunications"|industry_text_f=="transportation"|industry_text_f=="utility"
replace industry_f="7" if industry_text_f=="travel"
replace industry_f="5" if industry_text_f=="veterinary"
replace industry_f="6" if industry_text_f=="wholesale"


/* Not possible to classify the following, mostly unclear whether production or distribution etc.:

AUTOMOTIVE
Accounting
Administration
Analytics
Arts
Auto
Chemiclas
Children activities
Computer Software
Computer software
Consumer packaged goods
Consumer services
Content Management
Customer service
Durable goods
ELECTRONICS
Electronic Funds Transfer - Software,..
Energy
Entertainment
Environmental
Field service of automated industrial..
Fine wines
First time
Food and Beverage
Graduate student in chemistry
GIS
HI tech
High Tech
Human Resources
Industrial Manufacturing
Industrial manufacturing
Industry
Infrastructure
Internet
Manager
Manufactoring
Manufacturing
Marketing
Media
Medical Software
Military contractor
NA
Non Profit
Non for Profit
Non profit
Non-profit
Non_Profit
Nonprofit
Office Suites
Oil and Gas
Oil/Gas
Pharmaceuticals
Product Support
Professional
Public Affairs
R&D
Renewables
Security
Staffing
TEXTILES
Technology
accountingagencyartsauditautomationautomotiveautomotive partsbusinessceochefclubingcompliance
consumer electronicsconsumer servicescustomer servicecustomer services dd design dry cleanerenvironmentenvironmental
food
food and beverage
hr
human resources
human service
industrial
industrial manufacturing
manufacturing
manufacturing - commercial/industrial
manufacturting
maritime
medical devices
namufacturing
no
non profit
non-profit
nonprofit
not sure
oil and gas
ok
"online gaming"
pharma
wholesale distribution
sales
sales to retail
technology
technology staffing
telecommunications technology
trans
transportation planning
unsure
white coller
z
						   
*/								  

replace joblossprob_post="" if joblossprob_post=="NA"
destring *, replace

gen t_start = d_start*24*60*60 + h_start*60*60 + m_start*60 + s_start
gen t_end = d_end*24*60*60 + h_end*60*60 + m_end*60 + s_end
gen duration_f = t_end - t_start
tab duration_f
la var duration_f "Duration in seconds (Follow-up)"

keep rnid finished ppc prior_recession joblossprob_post increaseunempl_prob increase_localunempl jobfindingprob_post ///
posterior_recession wave rnid q1 q146_1_text q146_2_text q146_3_text q146_4_text q146_5_text q146_6_text q146_7_text ///
q2 consent q28 fin_prospects q29 ///
incomeexpectations_4 incomeexpectations_6 incomeexpectations_7 incomeexpectations_8 incomeexpectations_9 incomeexpectations_10 ///
incomeexpectations_11 incomeexpectations_13 incomeexpectations_14 incomeexpectations_15 incomeexpectations_16 incomeexpectations_17 ///
q54 jobfinding_prob_1 profitability v59 q55 ///
q38 q40_1 q40_2 q40_3 q40_4 q58 stocktrade durablepurchase q31_1 q31_2 q31_3 q31_4 q31_5 employerexp ///
causes_1 causes_2 causes_3 causes_4 causes_5 causes_6 causes_7 causes_8 causes_9 causes_10 causes_11 causes_12 ///
causes_13 causes_14 causes_15 causes_16 causes_16_text unemploymenthistory industry_f industry_text_f ///
mainearner earnings_willing currentearnings zipcode_f q144 q98 locationlatitude locationlongitude locationaccuracy ///
v106 d_start h_start m_start s_start d_end h_end m_end s_end t_start t_end duration_f

rename posterior_recession posterior_f
la var posterior "Posterior belief: Recession (Follow-up)"

rename increaseunempl_prob unemp_prob_f

recode fin_prospects (8=2) (2=3) (3=4) (4=5) (7=6) (6=7), gen(fin_prosp_f)

gen earn_mean_f = (incomeexpectations_4*18 + incomeexpectations_6*14 + ///
incomeexpectations_7*10 + incomeexpectations_8*6 + incomeexpectations_9*3 + incomeexpectations_10*1 - ///
incomeexpectations_11*1 - incomeexpectations_13*3 - incomeexpectations_14*6 - ///
incomeexpectations_15*10 - incomeexpectations_16*14 - incomeexpectations_17*18)/100

gen earn_sd_f = sqrt((incomeexpectations_4*(18 - earn_mean)^2  + incomeexpectations_6*(14 - earn_mean)^2 + ///
incomeexpectations_7*(10 - earn_mean)^2 + incomeexpectations_8*(6 - earn_mean)^2 + incomeexpectations_9*(3 - earn_mean)^2 + ///
incomeexpectations_10*(1 - earn_mean)^2 + incomeexpectations_11*(-1 - earn_mean)^2 + incomeexpectations_13*(-3 - earn_mean)^2 + ///
incomeexpectations_14*(-6 - earn_mean)^2 + incomeexpectations_15*(-10 - earn_mean)^2 + incomeexpectations_16*(-14 - earn_mean)^2 + incomeexpectations_17*(-18 - earn_mean)^2)/100)

gen earn_num_bins_f=0 if earn_mean_f!=.
foreach i in 4 6 7 8 9 10 11 13 14 15 16 17{
replace earn_num_bins_f=earn_num_bins_f+1 if incomeexpectations_`i'>0 & earn_mean_f!=.
}

gen earn_noncontiguous_f=0 if earn_mean_f!=.
forvalues i=1/2{
gen switch_pos_`i'=0 if earn_mean_f!=.
gen switch_neg_`i'=0 if earn_mean_f!=.
}
foreach i in 4 6 7 8 9 10 11 13 14 15 16 17{
replace switch_pos_1=`i' if switch_pos_1==0 & incomeexpectations_`i'>0
}
foreach i in 6 7 8 9 10 11 13 14 15 16 17{
replace switch_neg_1=`i' if switch_neg_1==0 & incomeexpectations_`i'==0 & switch_pos_1<`i'
}
foreach i in 7 8 9 10 11 13 14 15 16 17{
replace switch_pos_2=`i' if switch_pos_2==0 & incomeexpectations_`i'>0 & switch_pos_1<`i' & switch_neg_1<`i' & switch_pos_1!=0 & switch_neg_1!=0
}
replace earn_noncontiguous_f=1 if switch_pos_2>0 & earn_mean_f!=.
drop switch_*

gen earn_f_bin10=incomeexpectations_4+incomeexpectations_6
rename incomeexpectations_7 earn_f_bin9
rename incomeexpectations_8 earn_f_bin8
rename incomeexpectations_9 earn_f_bin7
rename incomeexpectations_10 earn_f_bin6
rename incomeexpectations_11 earn_f_bin5
rename incomeexpectations_13 earn_f_bin4
rename incomeexpectations_14 earn_f_bin3
rename incomeexpectations_15 earn_f_bin2
gen earn_f_bin1=incomeexpectations_16+incomeexpectations_17

rename joblossprob_post jobloss_f
rename jobfinding_prob_1 jobfind_f

gen jobinsecurity_f=(0.01*jobloss_f)*(0.01*(100-jobfind_f))*100

gen profits_f=8-profitability

rename increase_localunempl unemp_county_prob_f

gen spending_f=q58 if q58!=. & q58>=0
gen log_spending_f=log(q58+1)

gen durablepurchase_f=(durablepurchase==1) if durablepurchase!=.
gen stocktrade_f=6-stocktrade if stocktrade!=.
gen stocks_incr_f=(stocktrade_f>=4) if stocktrade_f!=.
gen stocks_decr_f=(stocktrade_f<=2) if stocktrade_f!=.

gen follownews_f=q31_1 
gen constr_exp_f=q31_5

gen exp_emp_f=6-employerexp

la var unemp_prob_f "Higher unemployment: Probability (Follow-up)"
la var unemp_county_prob_f "Higher unemployment in own county: Probability (Follow-up)"

la var fin_prosp_f "Financial prospects: Categorical (Follow-up)"
la var earn_mean_f "Earnings expectations: Mean (Follow-up)"
la var earn_sd_f "Earnings expectations: Std. dev. (Follow-up)"
la var earn_num_bins_f "Earnings expectations: Number of bins with pos. prob. (Follow-up)"
la var earn_noncontiguous_f "Earnings expectations: Pos. prob. in non-contiguous bins (Follow-up)"
la var jobloss_f "Job loss: Probability (Follow-up)"
la var jobfind_f "Job finding: Probability (Follow-up)"
la var jobinsecurity_f "Job insecurity (composite) (Follow-up)"

la var spending_f "Non-durable spending last week (Follow-up)"
la var log_spending_f "Log(Non-durable spending last week) (Follow-up)"
la var durablepurchase_f "Bought durable goods"
la var stocktrade_f "Increase in stockholdings"
la var stocks_incr_f "Increased stockholdings"
la var stocks_decr_f "Decreased stockholdings"

la var profits_f "Higher profitability all firms: Categorical (Follow-up)"

la var follownews_f "Followed news about the economy last week (Follow-up)"
la var constr_exp_f "Credit constraint expectations (Follow-up)"
la var exp_emp_f "Higher employment own employer: Categorical (Follow-up)"

gen emp_hiring=q31_2-14
la var emp_hiring "Employer's hiring depends on aggregate economy"
gen emp_firing=q31_3-14
la var emp_firing "Employer's firing depends on aggregate economy"
gen emp_profits=q31_4-14
la var emp_profits "Employer's profits depend on aggregate economy"

gen cause_irates=(causes_1==1)
la var cause_irates "Recession cause: Interest rate increase"
gen cause_oilprice=(causes_2==1)
la var cause_oilprice "Recession cause: Oil price increase"
gen cause_terror=(causes_3==1)
la var cause_terror "Recession cause: Terrorist attack"
gen cause_govspend=(causes_4==1)
la var cause_govspend "Recession cause: Government spending cut"
gen cause_stockmarket=(causes_5==1)
la var cause_stockmarket "Recession cause: Stock market crash"
gen cause_exports=(causes_6==1)
la var cause_exports "Recession cause: Lower export demand"
gen cause_intermediateimports=(causes_7==1)
la var cause_intermediateimports "Recession cause: Higher prices of imported intermediate goods"
gen cause_commodities=(causes_8==1)
la var cause_commodities "Recession cause: Increase in commodity prices"
gen cause_consumerconf=(causes_9==1)
la var cause_consumerconf "Recession cause: Drop in consumer confidence"
gen cause_businessconf=(causes_10==1)
la var cause_businessconf "Recession cause: Drop in business confidence"
gen cause_naturaldisaster=(causes_11==1)
la var cause_naturaldisaster "Recession cause: Natural disaster"
gen cause_housepricedrop=(causes_12==1)
la var cause_housepricedrop "Recession cause: House price drop"
gen cause_taxhikes=(causes_13==1)
la var cause_taxhikes "Recession cause: Tax hikes"
gen cause_political=(causes_14==1)
la var cause_political "Recession cause: Political turmoil"
gen cause_strikes=(causes_15==1)
la var cause_strikes "Recession cause: Strikes"
gen cause_other=(causes_16==1)
la var cause_other "Recession cause: Other"

gen ever_unemployed=(unemploymenthistory==4) if unemploymenthistory!=.
la var ever_unemployed "Ever involuntarily unemployed"

la var industry_f "Industry (Follow-up)"

gen construction_f=(industry_f==1) if industry_f!=.
la var construction_f "Industry: Construction (Follow-up)"
gen manuf_durable_f=(industry_f==2) if industry_f!=.
la var manuf_durable_f "Industry: Durable manufacturing (Follow-up)"
gen finance_f=(industry_f==3) if industry_f!=.
la var finance_f "Industry: Finance (Follow-up)"
gen health_educ_f=(industry_f==4) if industry_f!=.
la var health_educ_f "Industry: Health and education (Follow-up)"
gen manuf_nondurable_f=(industry_f==5) if industry_f!=.
la var manuf_nondurable_f "Industry: Nondurable manufacturing (Follow-up)"
gen retail_f=(industry_f==6) if industry_f!=.
la var retail_f "Industry: Retail and wholesale (Follow-up)"
gen services_f=(industry_f==7) if industry_f!=.
la var services_f "Industry: Services (Follow-up)"
gen transportation_f=(industry_f==8) if industry_f!=.
la var transportation_f "Industry: Transportation (Follow-up)"
gen ind_other_f=(industry_f==9) if industry_f!=.
la var ind_other_f "Industry: Other (Follow-up)"

gen ind_check=construction+manuf_durable+finance+health_educ+manuf_nondurable+retail+services+transportation+ind_other
tab ind_check, m

la var industry_text_f "Industry (Text entry) (Follow-up)"

replace mainearner=(mainearner==1) if mainearner!=.
la var mainearner "Main earner in the household"

rename currentearnings earn_current_f
la var earn_current_f "Current monthly earnings (Follow-up)"

la var rnid "RN ID"

global varlist_f rnid duration rnid posterior_f unemp_prob_f unemp_county_prob_f fin_prosp_f earn_mean_f earn_sd_f earn_num_bins_f ///
earn_noncontiguous_f jobloss_f jobfind_f jobinsecurity_f profits_f spending_f log_spending_f follownews_f constr_exp_f exp_emp_f ///
emp_hiring emp_firing emp_profits ///
spending_f log_spending_f durablepurchase_f stocktrade_f stocks_incr_f stocks_decr_f ///
cause_irates cause_oilprice cause_terror cause_govspend cause_stockmarket cause_exports cause_intermediateimports ///
cause_commodities cause_consumerconf cause_businessconf cause_naturaldisaster cause_housepricedrop ///
cause_taxhikes cause_political cause_strikes cause_other ///
ever_unemployed ///
industry_f industry_text_f construction_f manuf_durable_f finance_f health_educ_f manuf_nondurable_f retail_f services_f transportation_f ind_other_f ///
mainearner earn_current_f zipcode_f ///
earn_f_bin1 earn_f_bin2 earn_f_bin3 earn_f_bin4 earn_f_bin5 earn_f_bin6 earn_f_bin7 earn_f_bin8 earn_f_bin9 earn_f_bin10

order $varlist_f
keep $varlist_f

tostring rnid, replace

cd "$path_data_processed"
save "experimentRN2.dta", replace




******************************************************************
***** 3) Main experiment: Matching main and follow-up survey *****
******************************************************************

clear
cd "$path_data_raw"
insheet using "ids.csv", names

tostring rnid, replace
qbys rnid: gen n = _n
tab n
keep if n==1

duplicates drop rnid, force

cd "$path_data_processed"
merge 1:n rnid using "experimentRN2.dta", force
drop _merge

drop rnid 
rename oldids rnid

qbys rnid: gen n2 = _n

tab n

keep if n==1

duplicates drop rnid, force

cd "$path_data_processed"
merge 1:n rnid using "experimentRN1.dta", force
drop _merge

gen D_log_spending_f = log_spending_f - log_spending
winsor D_log_spending_f, p(.01) gen(D_log_spending_f_w01), if posterior!=.
winsor D_log_spending_f, p(.02) gen(D_log_spending_f_w02), if posterior!=.
winsor D_log_spending_f, p(.05) gen(D_log_spending_f_w05), if posterior!=.
foreach i in 2 3{
gen D_log_spending_f_t`i' = D_log_spending_f
replace D_log_spending_f_t`i'=`i' if D_log_spending_f>`i' & D_log_spending_f!=.
replace D_log_spending_f_t`i'=-`i' if D_log_spending_f<-`i' & D_log_spending_f!=.
}

la var D_log_spending_f "Non-durable spending growth"
la var D_log_spending_f_w01 "Non-durable spending growth (wins. at 1 perc.)"
la var D_log_spending_f_w02 "Non-durable spending growth (wins. at 2 perc.)"
la var D_log_spending_f_w05 "Non-durable spending growth (wins. at 5 perc.)"
la var D_log_spending_f_t2 "Non-durable spending growth (top-coded at +/-2)"
la var D_log_spending_f_t3 "Non-durable spending growth (top-coded at +/-3)"

gen zipcodematch = zipcode==zipcode_f if zipcode_f!=.
tab zipcodematch // note that the zipcodes are identical for > 90 percent of the sample.

foreach var of varlist  unemp_prob_f unemp_county_prob_f ///
fin_prosp_f earn_mean_f earn_sd_f jobloss_f jobfind_f jobinsecurity_f ///
profits_f follownews_f constr_exp_f exp_emp_f ///
D_log_spending_f D_log_spending_f_w01 D_log_spending_f_w02 D_log_spending_f_w05 D_log_spending_f_t2 D_log_spending_f_t3 durablepurchase_f stocktrade_f stocks_incr_f stocks_decr_f {
destring `var', replace force
qui sum `var'
gen z_`var'=((`var'-r(mean))/r(sd))
}
destring posterior_f, replace force

la var z_unemp_prob_f "Higher unemployment: Probability (Follow-up) (z-scored)"
la var z_unemp_county_prob_f "Higher unemploymentin own county: Probability (Follow-up) (z-scored)"

la var z_fin_prosp_f "Financial prospects: Categorical (Follow-up) (z-scored)"
la var z_earn_mean_f "Earnings expectations: Mean (Follow-up) (z-scored)"
la var z_earn_sd_f "Earnings expectations: Std. dev. (Follow-up) (z-scored)"
la var z_jobloss_f "Job loss: Probability (Follow-up) (z-scored)"
la var z_jobfind_f "Job finding: Probability (Follow-up) (z-scored)"
la var z_jobinsecurity_f "Job insecurity (composite) (Follow-up) (z-scored)"

la var z_profits_f "Higher profitability all firms: Categorical (Follow-up) (z-scored)"

la var z_follownews_f "Followed news about the economy last week (Follow-up) (z-scored)"
la var z_constr_exp_f "Credit constraint expectations (Follow-up) (z-scored)"
la var z_exp_emp_f "Higher employment own employer: Categorical (Follow-up) (z-scored)"

la var z_D_log_spending_f "Non-durable spending growth (z-scored)"
la var z_D_log_spending_f_w01 "Non-durable spending growth (wins. at 1 perc.) (z-scored)"
la var z_D_log_spending_f_w02 "Non-durable spending growth (wins. at 2 perc.) (z-scored)"
la var z_D_log_spending_f_w05 "Non-durable spending growth (wins. at 5 perc.) (z-scored)"
la var z_D_log_spending_f_t2 "Non-durable spending growth (top-coded at +/-2) (z-scored)"
la var z_D_log_spending_f_t3 "Non-durable spending growth (top-coded at +/-3) (z-scored)"
la var z_durablepurchase_f "Bought durable goods"
la var z_stocktrade_f "Increase in stockholdings"
la var z_stocks_incr_f "Increased stockholdings"
la var z_stocks_decr_f "Decreased stockholdings"

gen complete_followup = posterior_f!=.
la var complete_followup "Completed the follow-up"


***** Industry coding *****

* a) Use follow-up industry classification if available, which apparentely made it easier for people to classify themselves:

foreach var in construction manuf_durable finance health_educ manuf_nondurable retail services transportation ind_other{
replace `var'=`var'_f if `var'_f!=.
}

* b) Now look at text entry in baseline and follow-up for missing cases:

list industry_text industry_text_f if construction==.
replace industry=7 if industry_text=="Medical Software"|industry_text=="Software"|industry_text=="travel"
replace services=1 if industry_text=="Medical Software"|industry_text=="Software"|industry_text=="travel"
foreach var in construction manuf_durable finance health_educ manuf_nondurable retail /*services*/ transportation ind_other{
replace `var'=0 if industry_text=="Medical Software"|industry_text=="Software"|industry_text=="travel"
}

replace industry=2 if industry_text=="lighting"
replace manuf_durable=1 if industry_text=="lighting"
foreach var in construction /*manuf_durable*/ finance health_educ manuf_nondurable retail services transportation ind_other{
replace `var'=0 if industry_text=="lighting"
}

drop industry_f industry_text_f construction_f manuf_durable_f finance_f health_educ_f manuf_nondurable_f retail_f services_f transportation_f ind_other_f


*** Earnings coding *****

/*
Problem: Many people gave a wrong interpretation to the earnings question in the follow-up.
I.e. they thought it is yearly, while it actually is monthly.
*/

* a) Comparing current earnings with total household income allows to disentangle interpretations as yearly from interpretations as monthly earnings:
list earn_current income if earn_2016_level==. & earn_current!=.

* b) For those whose earnings were still missing:
replace earn_current=4916.6667 if earn_2016_level==. & earn_current==59000
replace earn_current=6250 if earn_2016_level==. & earn_current==75000
replace earn_current=1750 if earn_2016_level==. & earn_current==21000
replace earn_current=2666.6667 if earn_2016_level==. & earn_current==32000
replace earn_current=4821.5833 if earn_2016_level==. & earn_current==57859
replace earn_current=12500 if earn_2016_level==. & earn_current==150000
replace earn_current=7500 if earn_2016_level==. & earn_current==90000
replace earn_current=3750 if earn_2016_level==. & earn_current==45000

* c) For all:
list earn_current income mainearner

replace earn_current=. if earn_current<500
replace earn_current=416.66667 if earn_current==5000 & income==7500
replace earn_current=750 if earn_current==9000 & income==20000
replace earn_current=1016.5833 if earn_current==12199 & income==37500
replace earn_current=1250 if earn_current==15000 & income==7500
replace earn_current=1250 if earn_current==15000 & income==37500
replace earn_current=1250 if earn_current==15000 & income==20000
replace earn_current=1666.6667 if earn_current==20000 & income==37500
replace earn_current=1750 if earn_current==21000 & income==20000
replace earn_current=1935 if earn_current==23220 & income==20000
replace earn_current=2000 if earn_current==24000 & income==20000
replace earn_current=2083.3333 if earn_current==25000 & income<250000

gen earn_g25000=(earn_current>25000) if earn_current!=.
replace earn_current=earn_current/12 if earn_current>25000

list earn_current income mainearner if earn_g25000==1
list earn_current earn_2016_level income mainearner if earn_g25000==1 & income==250000

replace earn_current=2777.75 if earn_2016_level==400000 & income==250000
drop earn_g25000

replace earn_current=earn_current*12 // re-scaling to yearly earnings
gen log_earn_current_f=log(earn_current)
la var log_earn_current_f "Log(Current monthly earnings (Follow-up))"

* d) Generate an earnings variable using current earnings where available and yearly earnings where current earnings are unavailable:

/* GDP deflator adjustment:

Annual average 2016: 111.441
First quarter 2017: 112.846
Deflate current earnings by: (112.846/111.441)=1.0126076

*/

gen earnings=earn_current/1.0126076 if earn_current!=.
replace earnings=earn_2016_level if earnings==. & earn_2016_level!=.
replace earnings=. if earnings<1000 // setting unrealistically low earnings to missing

* Imputing earnings for missings:
reg earnings inc_15000 inc_15000_25000 inc_25000_50000 inc_50000_75000 inc_75000_100000 inc_100000_150000 inc_150000_200000 inc_200000 inc_NR, nocons
predict earnings_pred, xb
replace earnings=earnings_pred if earnings==.
la var earnings "Yearly earnings"



***** Re-inflate earnings to 2017 dollars and generate categorical earnings variable here *****

replace earnings=earnings*1.0126076
gen log_earnings=log(earnings)
la var log_earnings "Log(Yearly earnings)"

gen earn_8000=(earnings<8000) if earnings!=.&earnings>0
la var earn_8000 "Earnings < 8,000"
gen earn_8000_16000=(earnings>=8000&earnings<16000) if earnings!=.&earnings>0
la var earn_8000_16000 "8,000 < Earnings < 16,000"
gen earn_16000_25000=(earnings>=16000&earnings<25000) if earnings!=.&earnings>0
la var earn_16000_25000 "16,000 < Earnings < 25,000"
gen earn_25000_35000=(earnings>=25000&earnings<35000) if earnings!=.&earnings>0
la var earn_25000_35000 "25,000 < Earnings < 35,000"
gen earn_35000_42500=(earnings>=35000&earnings<42500) if earnings!=.&earnings>0
la var earn_35000_42500 "35,000 < Earnings < 42,500"
gen earn_42500_60000=(earnings>=42500&earnings<60000) if earnings!=.&earnings>0
la var earn_42500_60000 "42,500 < Earnings < 60,000"
gen earn_60000_100000=(earnings>=60000&earnings<100000) if earnings!=.&earnings>0
la var earn_60000_100000 "60,000 < Earnings < 100,000"
gen earn_100000=(earnings>=100000) if earnings!=.&earnings>0
la var earn_100000 "Earnings > 100,000"

gen earn_check=earn_8000+earn_8000_16000+earn_16000_25000+earn_25000_35000+earn_35000_42500+earn_42500_60000+earn_60000_100000+earn_100000
tab earn_check, m



***** Dummies for heterogeneity analysis and other remaining variables:

drop if posterior==.
 
gen updating=posterior-prior
la var updating "Updating (recession)"

gen updating_f=posterior_f-prior
la var updating_f "Updating (recession) (Follow-up)"

replace finlit_index=finlit_index/3


/* Classifying recession causes:

- Only classify those as supply/demand where it is 100 percent clear.
- Demand side: Terror attack, cut in government spending, stock market crash, export demand shock, drop in consumer confidence,
  drop in business confidence, house price drop.
- Supply side: Oil price hike, increasing import prices of intermediate goods, commodity price increase, strikes.
- Potentially hitting both: Interest rate increase, natural disaster, tax hikes, political turmoil.

*/

gen cause_supply_num=cause_oilprice+cause_intermediateimports+cause_commodities+cause_strikes
gen cause_demand_num=cause_terror+cause_govspend+cause_stockmarket+cause_exports+cause_consumerconf+cause_businessconf+cause_housepricedrop

tab cause_supply_num
tab cause_demand_num

la var cause_supply_num "Recession causes: Number of supply-side mentions"
la var cause_demand_num "Recession causes: Number of demand-side mentions"

gen cause_demand=(cause_demand_num>=2) if cause_demand_num!=.
la var cause_demand "Recession cause: Demand-side mentioned at least twice"

la var zipcode_f "Zip code (Follow-up)"

summ earnings, d, if earnings!=.
gen highearn=(earnings>=r(p50)) if earnings!=.
la var highearn "High yearly earnings"

summ income, d, if inc_NR==0
gen highinc=(income>=r(p50)) if inc_NR==0
la var highinc "High income"

summ assets, d, if assets!=.
gen highassets=(assets>=r(p50)) if assets!=.
la var highassets "High assets"

summ unemp_rate2016, d, if unemp_rate2016!=.
gen highunemp_rate=(unemp_rate2016>=r(p50)) if unemp_rate2016!=.
la var highunemp_rate "High local unemployment rate (2016)"

summ prior, d, if prior!=.
gen highprior=(prior>=r(p50)) if prior!=.
la var highprior "High prior"

gen shock_neg=(shock<0) if shock!=.
la var shock_neg "Perception gap<0"

summ tenure, d, if tenure!=.
gen hightenure=(tenure>=r(p50)) if tenure!=.
la var hightenure "High tenure"

summ hours, d, if hours!=.
gen highhours=(hours>=r(p50)) if hours!=.
la var highhours "High hours"

gen old=(age>=45) if age!=.
replace old=1 if age==. & (age_45_54+age_55_64+age_65==1)
replace old=0 if age==. & (age_18_24+age_25_34+age_35_44==1)
la var old "Old"

rename unemp_rate_incr_1y unempincr_1y
rename unemp_rate_incr_2y unempincr_2y
rename unemp_rate_incr_3y unempincr_3y
rename unemp_rate_incr_5y unempincr_5y
rename unemp_rate_incr_10y unempincr_10y
rename unemp_rate_incr_15y unempincr_15y

gen firm_insurance_resc=(emp_profits-emp_firing+4)/10
la var firm_insurance_resc "Firm insurance (rescaled)"
gen emp_firing_resc=emp_firing/7
la var emp_firing_resc "Employer's firing depends on aggregate economy (rescaled)"

gen never_unemployed=1-ever_unemployed if ever_unemployed!=.
la var never_unemployed "Never unemployed"

* Imputation: Ever unemployed:
foreach var of varlist tenure tenure_1 tenure_1_2 tenure_2_3 tenure_3_4 tenure_4_5 tenure_5_10 tenure_10_20 tenure_20 unconstrained assets log_assets{
gen mis_`var'=(`var'==.)
replace `var'=0 if `var'==.
}
logit never_unemployed age_25_34 age_35_44 age_45_54 age_55_64 age_65 ///
tenure_1_2 tenure_2_3 tenure_3_4 tenure_4_5 tenure_5_10 tenure_10_20 tenure_20 mis_tenure unconstrained mis_unconstrained ///
log_assets mis_assets, r
predict never_unemployed_pred, pr
tab never_unemployed_pred if never_unemployed==0
tab never_unemployed_pred if never_unemployed==1
tab never_unemployed if never_unemployed_pred<=.5
tab never_unemployed if never_unemployed_pred>.5
gen correct_prediction=((never_unemployed==1&never_unemployed_pred>.5)|(never_unemployed==0&never_unemployed_pred<=.5)) if never_unemployed!=.
tab correct_prediction
tab correct_prediction if never_unemployed==0
tab correct_prediction if never_unemployed==1
gen never_unemployed_imp=never_unemployed
replace never_unemployed_imp=1 if never_unemployed_pred>.5 & never_unemployed==.
replace never_unemployed_imp=0 if never_unemployed_pred<=.5 & never_unemployed==.
gen ever_unemployed_imp=1-never_unemployed_imp if never_unemployed_imp!=.
foreach var of varlist tenure tenure_1 tenure_1_2 tenure_2_3 tenure_3_4 tenure_4_5 tenure_5_10 tenure_10_20 tenure_20 unconstrained assets log_assets{
replace `var'=. if mis_`var'==1
drop mis_`var'
}
la var ever_unemployed_imp "Ever unemployed (imputed)"
la var never_unemployed_imp "Never unemployed (imputed)"


foreach var in follow_news unconstrained emp_hiring emp_firing emp_profits ///
unempincr_1y unempincr_3y unempincr_5y unempincr_10y unemp_rate3y{
summ `var', d, if `var'!=.
gen high`var'=(`var'>=r(p50)) if `var'!=.
}
la var highunemp_rate3y "High county-level unemployment last 3 yrs."
la var highfollow_news "High follow news about the economy"
la var highunconstrained "High unconstrained"
la var highemp_hiring "High dependence of employer's hiring on aggregate economy"
la var highemp_firing "High dependence of employer's firing on aggregate economy"
la var highemp_profits "High dependence of employer's profits on aggregate economy"
la var highunempincr_1y "High incr. in county-level unemployment over last yr."
la var highunempincr_3y "High incr. in county-level unemployment over last 3 yrs."
la var highunempincr_5y "High incr. in county-level unemployment over last 5 yrs."
la var highunempincr_10y "High incr. in county-level unemployment over last 10 yrs."

gen mainearner_imp=mainearner
replace mainearner_imp=0 if earnings/income<0.5 &mainearner==.
replace mainearner_imp=1 if earnings/income>=0.5 &mainearner==.
la var mainearner_imp "Main earner in the household (imputed)"

gen nonmainearner=1-mainearner
gen nonmainearner_imp=1-mainearner_imp
la var nonmainearner "Not main earner in the household"
la var nonmainearner_imp "Not main earner in the household (imputed)"

gen shock_timeontreatmentpage = shock*timeontreatmentpage

***** Clean up things:

global allvars ///
rnid duration duration_f attentive prior prior_conf posterior timeontreatmentpage shock_timeontreatmentpage ///
treatment shock shock_neg updating ///
unemp_cat unemp_prob unemp_county_prob ///
fin_prosp earn_mean earn_sd earn_num_bins earn_noncontiguous jobloss jobfind jobinsecurity ///
infl_mean infl_sd infl_num_bins infl_noncontiguous ///
durables food_home food_away leisure spending log_spending ///
profits ///
z_unemp_cat z_unemp_prob z_unemp_county_prob ///
z_fin_prosp z_earn_mean z_earn_sd z_jobloss z_jobfind z_jobinsecurity ///
z_infl_mean z_infl_sd ///
z_durables z_food_home z_food_away z_leisure z_cons_index_A z_ndcons_index_A ///
z_profits  ///
geofips northeast midwest south west state statefips countyfips zipcode zipcode_f ///
female birth_year age agesq age_18_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65 old ///
below_highschool highschool some_college associate_degree bachelors_degree postgrad_degree atleast_bachelor ///
industry construction manuf_durable finance health_educ manuf_nondurable retail services transportation ind_other ///
earnings log_earnings ///
earn_8000 earn_8000_16000 earn_16000_25000 earn_25000_35000 earn_35000_42500 earn_42500_60000 earn_60000_100000 earn_100000 ///
earn_2016_wtr earn_2016_NR earn_2016_level log_earn_2016_level ///
earn_current log_earn_current ///
public_employer private_employer other_employer ///
tenure tenure_1 tenure_1_2 tenure_2_3 tenure_3_4 tenure_4_5 tenure_5_10 tenure_10_20 tenure_20 ///
hours hours_10 hours_10_20 hours_20_30 hours_30_40 hours_40_50 hours_50_60 hours_60 ///
single married separated divorced widowed marstat_other ///
finlit_int finlit_infl finlit_div finlit_index ///
follow_news unconstrained ///
republican democrat independent ///
famsize famsize_1 famsize_2 famsize_3 famsize_4 famsize_5 ///
income log_income inc_15000 inc_15000_25000 inc_25000_50000 inc_50000_75000 inc_75000_100000 inc_100000_150000 inc_150000_200000 inc_200000 inc_NR ///
assets log_assets assets_500 assets_500_1000 assets_1000_2000 assets_2000_5000 assets_5000_20000 assets_20000_50000 assets_50000_200000 assets_200000 assets_NR ///
unemp_rate2014 unemp_rate2015 unemp_rate2016 unemp_rate3y unempincr_1y unempincr_2y unempincr_3y unempincr_5y unempincr_10y unempincr_15y ///
complete_followup ///
posterior_f updating_f unemp_prob_f unemp_county_prob_f fin_prosp_f earn_mean_f earn_sd_f earn_num_bins_f earn_noncontiguous_f ///
jobloss_f jobfind_f jobinsecurity_f profits_f constr_exp_f exp_emp_f ///
spending_f log_spending_f D_log_spending_f D_log_spending_f_w01 D_log_spending_f_w02 D_log_spending_f_w05 D_log_spending_f_t2 D_log_spending_f_t3 durablepurchase_f stocktrade_f stocks_incr_f stocks_decr_f follownews_f ///
z_unemp_prob_f z_unemp_county_prob_f z_fin_prosp_f z_earn_mean_f z_earn_sd_f ///
z_jobloss_f z_jobfind_f z_jobinsecurity_f z_profits_f z_follownews_f z_constr_exp_f z_exp_emp_f ///
z_D_log_spending_f z_D_log_spending_f_w01 z_D_log_spending_f_w02 z_D_log_spending_f_w05 z_D_log_spending_f_t3 z_D_log_spending_f_t2 z_durablepurchase_f z_stocktrade_f z_stocks_incr_f z_stocks_decr_f z_follownews_f ///
emp_hiring emp_firing emp_profits firm_insurance_resc emp_firing_resc ///
cause_irates cause_oilprice cause_terror cause_govspend cause_stockmarket cause_exports cause_intermediateimports ///
cause_commodities cause_consumerconf cause_businessconf cause_naturaldisaster cause_housepricedrop ///
cause_taxhikes cause_political cause_strikes cause_other cause_supply_num cause_demand_num cause_demand ///
ever_unemployed never_unemployed ever_unemployed_imp never_unemployed_imp ///
mainearner mainearner_imp nonmainearner nonmainearner_imp ///
highunemp_rate3y highearn highinc highassets highunemp_rate highprior hightenure highhours ///
highfollow_news highunconstrained highemp_hiring highemp_firing highemp_profits highunempincr_1y highunempincr_3y highunempincr_5y highunempincr_10y ///
infl_bin1 infl_bin2 infl_bin3 infl_bin4 infl_bin5 infl_bin6 infl_bin7 infl_bin8 infl_bin9 infl_bin10 ///
earn_bin1 earn_bin2 earn_bin3 earn_bin4 earn_bin5 earn_bin6 earn_bin7 earn_bin8 earn_bin9 earn_bin10

order $allvars
keep $allvars

gen cell2=""


* Northeast:

replace cell2="a1g1i1e1r1" if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==0 & northeast==1
replace cell2="a1g1i1e2r1" if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==1 & northeast==1

replace cell2="a1g1i2e1r1" if age<=42 & female==0 & income>73864.85 & atleast_bachelor==0 & northeast==1
replace cell2="a1g1i2e2r1" if age<=42 & female==0 & income>73864.85 & atleast_bachelor==1 & northeast==1

replace cell2="a1g2i1e1r1" if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==0 & northeast==1
replace cell2="a1g2i1e2r1" if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==1 & northeast==1

replace cell2="a1g2i2e1r1" if age<=42 & female==1 & income>73864.85 & atleast_bachelor==0 & northeast==1
replace cell2="a1g2i2e2r1" if age<=42 & female==1 & income>73864.85 & atleast_bachelor==1 & northeast==1

replace cell2="a2g1i1e1r1" if age>42 & female==0 & income<=73864.85 & atleast_bachelor==0 & northeast==1
replace cell2="a2g1i1e2r1" if age>42 & female==0 & income<=73864.85 & atleast_bachelor==1 & northeast==1

replace cell2="a2g1i2e1r1" if age>42 & female==0 & income>73864.85 & atleast_bachelor==0 & northeast==1
replace cell2="a2g1i2e2r1" if age>42 & female==0 & income>73864.85 & atleast_bachelor==1 & northeast==1

replace cell2="a2g2i1e1r1" if age>42 & female==1 & income<=73864.85 & atleast_bachelor==0 & northeast==1
replace cell2="a2g2i1e2r1" if age>42 & female==1 & income<=73864.85 & atleast_bachelor==1 & northeast==1

replace cell2="a2g2i2e1r1" if age>42 & female==1 & income>73864.85 & atleast_bachelor==0 & northeast==1
replace cell2="a2g2i2e2r1" if age>42 & female==1 & income>73864.85 & atleast_bachelor==1 & northeast==1


* Midwest:

replace cell2="a1g1i1e1r2" if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==0 & midwest==1
replace cell2="a1g1i1e2r2" if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==1 & midwest==1

replace cell2="a1g1i2e1r2" if age<=42 & female==0 & income>73864.85 & atleast_bachelor==0 & midwest==1
replace cell2="a1g1i2e2r2" if age<=42 & female==0 & income>73864.85 & atleast_bachelor==1 & midwest==1

replace cell2="a1g2i1e1r2" if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==0 & midwest==1
replace cell2="a1g2i1e2r2" if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==1 & midwest==1

replace cell2="a1g2i2e1r2" if age<=42 & female==1 & income>73864.85 & atleast_bachelor==0 & midwest==1
replace cell2="a1g2i2e2r2" if age<=42 & female==1 & income>73864.85 & atleast_bachelor==1 & midwest==1

replace cell2="a2g1i1e1r2" if age>42 & female==0 & income<=73864.85 & atleast_bachelor==0 & midwest==1
replace cell2="a2g1i1e2r2" if age>42 & female==0 & income<=73864.85 & atleast_bachelor==1 & midwest==1

replace cell2="a2g1i2e1r2" if age>42 & female==0 & income>73864.85 & atleast_bachelor==0 & midwest==1
replace cell2="a2g1i2e2r2" if age>42 & female==0 & income>73864.85 & atleast_bachelor==1 & midwest==1

replace cell2="a2g2i1e1r2" if age>42 & female==1 & income<=73864.85 & atleast_bachelor==0 & midwest==1
replace cell2="a2g2i1e2r2" if age>42 & female==1 & income<=73864.85 & atleast_bachelor==1 & midwest==1

replace cell2="a2g2i2e1r2" if age>42 & female==1 & income>73864.85 & atleast_bachelor==0 & midwest==1
replace cell2="a2g2i2e2r2" if age>42 & female==1 & income>73864.85 & atleast_bachelor==1 & midwest==1
 

* South:

replace cell2="a1g1i1e1r3" if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==0 & south==1
replace cell2="a1g1i1e2r3" if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==1 & south==1

replace cell2="a1g1i2e1r3" if age<=42 & female==0 & income>73864.85 & atleast_bachelor==0 & south==1
replace cell2="a1g1i2e2r3" if age<=42 & female==0 & income>73864.85 & atleast_bachelor==1 & south==1

replace cell2="a1g2i1e1r3" if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==0 & south==1
replace cell2="a1g2i1e2r3" if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==1 & south==1

replace cell2="a1g2i2e1r3" if age<=42 & female==1 & income>73864.85 & atleast_bachelor==0 & south==1
replace cell2="a1g2i2e2r3" if age<=42 & female==1 & income>73864.85 & atleast_bachelor==1 & south==1

replace cell2="a2g1i1e1r3" if age>42 & female==0 & income<=73864.85 & atleast_bachelor==0 & south==1
replace cell2="a2g1i1e2r3" if age>42 & female==0 & income<=73864.85 & atleast_bachelor==1 & south==1

replace cell2="a2g1i2e1r3" if age>42 & female==0 & income>73864.85 & atleast_bachelor==0 & south==1
replace cell2="a2g1i2e2r3" if age>42 & female==0 & income>73864.85 & atleast_bachelor==1 & south==1

replace cell2="a2g2i1e1r3" if age>42 & female==1 & income<=73864.85 & atleast_bachelor==0 & south==1
replace cell2="a2g2i1e2r3" if age>42 & female==1 & income<=73864.85 & atleast_bachelor==1 & south==1

replace cell2="a2g2i2e1r3" if age>42 & female==1 & income>73864.85 & atleast_bachelor==0 & south==1
replace cell2="a2g2i2e2r3" if age>42 & female==1 & income>73864.85 & atleast_bachelor==1 & south==1


* West:

replace cell2="a1g1i1e1r4" if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==0 & west==1
replace cell2="a1g1i1e2r4" if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==1 & west==1

replace cell2="a1g1i2e1r4" if age<=42 & female==0 & income>73864.85 & atleast_bachelor==0 & west==1
replace cell2="a1g1i2e2r4" if age<=42 & female==0 & income>73864.85 & atleast_bachelor==1 & west==1

replace cell2="a1g2i1e1r4" if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==0 & west==1
replace cell2="a1g2i1e2r4" if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==1 & west==1

replace cell2="a1g2i2e1r4" if age<=42 & female==1 & income>73864.85 & atleast_bachelor==0 & west==1
replace cell2="a1g2i2e2r4" if age<=42 & female==1 & income>73864.85 & atleast_bachelor==1 & west==1

replace cell2="a2g1i1e1r4" if age>42 & female==0 & income<=73864.85 & atleast_bachelor==0 & west==1
replace cell2="a2g1i1e2r4" if age>42 & female==0 & income<=73864.85 & atleast_bachelor==1 & west==1

replace cell2="a2g1i2e1r4" if age>42 & female==0 & income>73864.85 & atleast_bachelor==0 & west==1
replace cell2="a2g1i2e2r4" if age>42 & female==0 & income>73864.85 & atleast_bachelor==1 & west==1

replace cell2="a2g2i1e1r4" if age>42 & female==1 & income<=73864.85 & atleast_bachelor==0 & west==1
replace cell2="a2g2i1e2r4" if age>42 & female==1 & income<=73864.85 & atleast_bachelor==1 & west==1

replace cell2="a2g2i2e1r4" if age>42 & female==1 & income>73864.85 & atleast_bachelor==0 & west==1
replace cell2="a2g2i2e2r4" if age>42 & female==1 & income>73864.85 & atleast_bachelor==1 & west==1

cd "$path_data_raw"
merge n:1 cell2 using "weights_ACS.dta"
drop _merge 



*** Calculating weights:

gen cons = 1

egen samplen2_1 = count(cons) if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==0 & northeast==1
egen samplen2_2 = count(cons) if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==1 & northeast==1
egen samplen2_3 = count(cons) if age<=42 & female==0 & income>73864.85 & atleast_bachelor==0 & northeast==1
egen samplen2_4 = count(cons)if age<=42 & female==0 & income>73864.85 & atleast_bachelor==1 & northeast==1
egen samplen2_5 = count(cons) if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==0 & northeast==1
egen samplen2_6 = count(cons) if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==1 & northeast==1
egen samplen2_7 = count(cons) if age<=42 & female==1 & income>73864.85 & atleast_bachelor==0 & northeast==1
egen samplen2_8 = count(cons) if age<=42 & female==1 & income>73864.85 & atleast_bachelor==1 & northeast==1
egen samplen2_9 = count(cons) if age>42 & female==0 & income<=73864.85 & atleast_bachelor==0 & northeast==1
egen samplen2_10 = count(cons) if age>42 & female==0 & income<=73864.85 & atleast_bachelor==1 & northeast==1
egen samplen2_11 = count(cons) if age>42 & female==0 & income>73864.85 & atleast_bachelor==0 & northeast==1
egen samplen2_12 = count(cons) if age>42 & female==0 & income>73864.85 & atleast_bachelor==1 & northeast==1
egen samplen2_13 = count(cons) if age>42 & female==1 & income<=73864.85 & atleast_bachelor==0 & northeast==1
egen samplen2_14 = count(cons) if age>42 & female==1 & income<=73864.85 & atleast_bachelor==1 & northeast==1
egen samplen2_15 = count(cons) if age>42 & female==1 & income>73864.85 & atleast_bachelor==0 & northeast==1
egen samplen2_16 = count(cons) if age>42 & female==1 & income>73864.85 & atleast_bachelor==1 & northeast==1
egen samplen2_17 = count(cons) if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==0 & midwest==1
egen samplen2_18 = count(cons) if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==1 & midwest==1
egen samplen2_19 = count(cons) if age<=42 & female==0 & income>73864.85 & atleast_bachelor==0 & midwest==1
egen samplen2_20 = count(cons) if age<=42 & female==0 & income>73864.85 & atleast_bachelor==1 & midwest==1
egen samplen2_21 = count(cons) if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==0 & midwest==1
egen samplen2_22 = count(cons) if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==1 & midwest==1
egen samplen2_23 = count(cons) if age<=42 & female==1 & income>73864.85 & atleast_bachelor==0 & midwest==1
egen samplen2_24 = count(cons) if age<=42 & female==1 & income>73864.85 & atleast_bachelor==1 & midwest==1
egen samplen2_25 = count(cons) if age>42 & female==0 & income<=73864.85 & atleast_bachelor==0 & midwest==1
egen samplen2_26 = count(cons) if age>42 & female==0 & income<=73864.85 & atleast_bachelor==1 & midwest==1
egen samplen2_27 = count(cons) if age>42 & female==0 & income>73864.85 & atleast_bachelor==0 & midwest==1
egen samplen2_28 = count(cons) if age>42 & female==0 & income>73864.85 & atleast_bachelor==1 & midwest==1
egen samplen2_29 = count(cons) if age>42 & female==1 & income<=73864.85 & atleast_bachelor==0 & midwest==1
egen samplen2_30 = count(cons) if age>42 & female==1 & income<=73864.85 & atleast_bachelor==1 & midwest==1
egen samplen2_31 = count(cons) if age>42 & female==1 & income>73864.85 & atleast_bachelor==0 & midwest==1
egen samplen2_32 = count(cons) if age>42 & female==1 & income>73864.85 & atleast_bachelor==1 & midwest==1
egen samplen2_33 = count(cons) if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==0 & south==1
egen samplen2_34 = count(cons) if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==1 & south==1
egen samplen2_35 = count(cons) if age<=42 & female==0 & income>73864.85 & atleast_bachelor==0 & south==1
egen samplen2_36 = count(cons) if age<=42 & female==0 & income>73864.85 & atleast_bachelor==1 & south==1
egen samplen2_37 = count(cons) if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==0 & south==1
egen samplen2_38 = count(cons) if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==1 & south==1
egen samplen2_39 = count(cons) if age<=42 & female==1 & income>73864.85 & atleast_bachelor==0 & south==1
egen samplen2_40 = count(cons) if age<=42 & female==1 & income>73864.85 & atleast_bachelor==1 & south==1
egen samplen2_41 = count(cons)  if age>42 & female==0 & income<=73864.85 & atleast_bachelor==0 & south==1
egen samplen2_42 = count(cons)  if age>42 & female==0 & income<=73864.85 & atleast_bachelor==1 & south==1 
egen samplen2_43 = count(cons)  if age>42 & female==0 & income>73864.85 & atleast_bachelor==0 & south==1
egen samplen2_44 = count(cons)  if age>42 & female==0 & income>73864.85 & atleast_bachelor==1 & south==1 
egen samplen2_45 = count(cons)  if age>42 & female==1 & income<=73864.85 & atleast_bachelor==0 & south==1
egen samplen2_46 = count(cons)  if age>42 & female==1 & income<=73864.85 & atleast_bachelor==1 & south==1 
egen samplen2_47 = count(cons)  if age>42 & female==1 & income>73864.85 & atleast_bachelor==0 & south==1
egen samplen2_48 = count(cons)  if age>42 & female==1 & income>73864.85 & atleast_bachelor==1 & south==1
egen samplen2_49 = count(cons)  if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==0 & west==1
egen samplen2_50 = count(cons)  if age<=42 & female==0 & income<=73864.85 & atleast_bachelor==1 & west==1
egen samplen2_51 = count(cons)  if age<=42 & female==0 & income>73864.85 & atleast_bachelor==0 & west==1
egen samplen2_52 = count(cons)  if age<=42 & female==0 & income>73864.85 & atleast_bachelor==1 & west==1
egen samplen2_53 = count(cons)  if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==0 & west==1
egen samplen2_54 = count(cons)  if age<=42 & female==1 & income<=73864.85 & atleast_bachelor==1 & west==1
egen samplen2_55 = count(cons)  if age<=42 & female==1 & income>73864.85 & atleast_bachelor==0 & west==1
egen samplen2_56 = count(cons)  if age<=42 & female==1 & income>73864.85 & atleast_bachelor==1 & west==1
egen samplen2_57 = count(cons)  if age>42 & female==0 & income<=73864.85 & atleast_bachelor==0 & west==1
egen samplen2_58 = count(cons)  if age>42 & female==0 & income<=73864.85 & atleast_bachelor==1 & west==1
egen samplen2_59 = count(cons)  if age>42 & female==0 & income>73864.85 & atleast_bachelor==0 & west==1
egen samplen2_60 = count(cons)  if age>42 & female==0 & income>73864.85 & atleast_bachelor==1 & west==1
egen samplen2_61 = count(cons)  if age>42 & female==1 & income<=73864.85 & atleast_bachelor==0 & west==1
egen samplen2_62 = count(cons)  if age>42 & female==1 & income<=73864.85 & atleast_bachelor==1 & west==1
egen samplen2_63 = count(cons)  if age>42 & female==1 & income>73864.85 & atleast_bachelor==0 & west==1
egen samplen2_64 = count(cons)  if age>42 & female==1 & income>73864.85 & atleast_bachelor==1 & west==1

gen samplen2=.
forvalues i = 1(1)64 {
replace samplen2 = samplen2_`i'/1124 if samplen2==.
}

gen pweight = cell2_weight/samplen2

drop if posterior==.
drop if prior==.
drop cons

cd "$path_data_processed"
save "experimentRNall.dta", replace



**************************************
***** 4) Robustness experiment 1 *****
**************************************

clear

cd "$path_data_raw"
insheet using "robustnessexperiment1.csv", names

cd "$path_data_processed"
save "robustnessexperiment1.dta", replace



**************************************
***** 5) Robustness experiment 2 *****
**************************************

clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user

set graphics on  

clear

cd "$path_data_raw"
insheet using "robustnessexperiment2.csv", names

drop in 1/2

gen demandtreatment = fl_178_do=="PriorRecession-demand"
gen control = fl_178_do=="Priorrecessioncontrol"
gen anchoring = fl_178_do=="PriorRecession-anchoring"

replace prior_recession="." if prior_recession=="NA"

destring *, replace

keep demandtreatment control anchoring fin_prospects cons_plans_1 cons_plans_2 cons_plans_4 prior_recession

rename cons_plans_1 food_home
rename cons_plans_2 food_away
rename cons_plans_4 leisure

foreach var of varlist food_home food_away leisure fin_prospects{
qui sum `var'
gen z_`var'=((`var'-r(mean))/r(sd))
}

gen z_ndcons_index_A=(1/3)*(z_food_home+z_food_away+z_leisure)

foreach var of varlist prior_recession z_fin_prospects z_ndcons_index_A{
reg `var' demandtreatment anchoring, r
}

cd "$path_data_processed"
save "robustnessexperiment2.dta", replace



**************************************
***** 6) Robustness experiment 3 *****
**************************************

clear

cd "$path_data_raw"
insheet using "robustnessexperiment3.csv", names
 
drop in 1/2

keep male age region hhincome educ emply conf unemployment usfirms confidence_posterior prior_recession increaseunempl_prob posterior_recession treatment

destring *, replace  force

gen t30 = treatment==1
gen t5 = treatment==2
gen t15avg = treatment==3
gen t15med = treatment==4
gen tanchor = treatment==5
gen t5noprior = treatment==6
gen control_prior = treatment==7
gen control_noprior = treatment==8

gen noprior = (treatment==6 | treatment==8)
gen noinfo = (treatment==5 | treatment==7 | treatment==8)

gen anyinfo = noinfo==0
gen t_optimism = anyinfo 

gen signal = 30 if treatment==1
replace signal = 5 if treatment==2 | treatment==6
replace signal = 15 if treatment==3
replace signal = 15 if treatment==4

rename prior_recession prior
rename posterior_recession posterior
gen updating=posterior-prior

gen prior_conf=6-conf
gen posterior_conf=6-confidence_posterior

rename increaseunempl_prob unemp_prob
gen profits=8-usfirms

la var prior "Prior belief: Recession"
la var posterior "Posterior belief: Recession"
la var prior_conf "Prior belief: Recession - Confidence"
la var posterior_conf "Posterior belief: Recession - Confidence"

la var unemp_prob "Higher unemployment: Probability"
la var profits "Higher profitability all firms: Categorical"

foreach var of varlist unemp_prob  ///
profits {
qui sum `var'
gen z_`var'=((`var'-r(mean))/r(sd))
}


la var z_unemp_prob "Higher unemployment: Probability (z-scored)"
la var z_profits "Higher profitability all firms: Categorical (z-scored)"

gen female = male==2
la var female "Female"

gen age_18_24=(age==1) if age!=.
la var age_18_24 "Age 18-24"
gen age_25_34=(age==2) if age!=.
la var age_25_34 "Age 25-34"
gen age_35_44=(age==3) if age!=.
la var age_35_44 "Age 35-44"
gen age_45_54=(age==4) if age!=.
la var age_45_54 "Age 45-54"
gen age_55_64=(age==5) if age!=.
la var age_55_64 "Age 55-64"
gen age_65=(age==6) if age!=.
la var age_65 "Age 65 and older"

gen age_check=age_18_24+age_25_34+age_35_44+age_45_54+age_55_64+age_65
tab age_check, m

gen northeast=(region==4) if region!=.
la var northeast "Northeast"
gen midwest=(region==6) if region!=.
la var midwest "Midwest"
gen south=(region==7) if region!=.
la var south "South"
gen west=(region==10) if region!=.
la var west "West"

gen region_check=northeast+midwest+south+west
tab region_check, m

gen inc_15000=(hhincome==1) if hhincome!=.
la var inc_15000 "Income < 15,000"
gen inc_15000_25000=(hhincome==4) if hhincome!=.
la var inc_15000_25000 "15,000 < Income < 25,000"
gen inc_25000_50000=(hhincome==6) if hhincome!=.
la var inc_25000_50000 "25,000 < Income < 50,000"
gen inc_50000_75000=(hhincome==3) if hhincome!=.
la var inc_50000_75000 "50,000 < Income < 75,000"
gen inc_75000_100000=(hhincome==2) if hhincome!=.
la var inc_75000_100000 "75,000 < Income < 100,000"
gen inc_100000_150000=(hhincome==9) if hhincome!=.
la var inc_100000_150000 "100,000 < Income < 150,000"
gen inc_150000_200000=(hhincome==11) if hhincome!=.
la var inc_150000_200000 "150,000 < Income < 200,000"
gen inc_200000=(hhincome==8) if hhincome!=.
la var inc_200000 "Income > 200,000"
gen inc_NR=(hhincome==7)
la var inc_NR "Income: Prefer not to say"

gen income_check=inc_15000+inc_15000_25000+inc_25000_50000+inc_50000_75000+inc_75000_100000+inc_100000_150000+inc_150000_200000+inc_200000+inc_NR
tab income_check, m

recode hhincome (1=7500) (4=20000) (6=37500) (3=62500) (2=87500) (9=125000) (11=175000) (8=250000) (7=.), gen(income)
la var income "Income"

gen log_income=log(income)
la var log_income "Log(Income)"

gen below_highschool=(educ==1) if educ!=.
la var below_highschool "Below highschool"
gen highschool=(educ==2) if educ!=.
la var highschool "Highschool"
gen some_college=(educ==3) if educ!=.
la var some_college "Some college"
gen associate_degree=(educ==4) if educ!=.
la var associate_degree "Associate degree"
gen bachelors_degree=(educ==5) if educ!=.
la var bachelors_degree "Bachelor's degree"
gen postgrad_degree=(educ==6) if educ!=.
la var postgrad_degree "Post-graduate degree"

gen educ_check=below_highschool+highschool+some_college+associate_degree+bachelors_degree+postgrad_degree
tab educ_check, m

gen atleast_bachelor=(educ>=5) if educ!=.
la var atleast_bachelor "At least bachelor's degree"

gen empl_ft=(emply==1) if emply!=.
la var empl_ft "Full-time employed"
gen empl_pt=(emply==2) if emply!=.
la var empl_pt "Part-time employed"
gen self_empl=(emply==9) if emply!=.
la var self_empl "Self-employed"
gen unempl=(emply==3) if emply!=.
la var unempl "Unemployed"
gen out_laborforce=(emply==4) if emply!=.
la var out_laborforce "Out of the labor force"
gen retired=(emply==5) if emply!=.
la var retired "Retired"
gen student=(emply==7) if emply!=.
la var student "Student"
gen empl_other=(emply==6) if emply!=.
la var empl_other "Other employment status"

gen empl_check=empl_ft+empl_pt+self_empl+unempl+out_laborforce+retired+student+empl_other
tab empl_check, m

gen empl=empl_pt+empl_ft if emply!=.
la var empl "Employed"

drop age
gen age=21 if age_18_24==1
replace age=30 if age_25_34==1
replace age=40 if age_35_44==1
replace age=50 if age_45_54==1
replace age=60 if age_55_64==1
replace age=70 if age_65==1
la var age "Age"

gen agesq=age^2
la var agesq "Age squared"

global vars ///
treatment t30 t5 t15avg t15med tanchor t5noprior control_prior control_noprior noprior noinfo anyinfo t_optimism signal ///
prior prior_conf posterior posterior_conf signal updating ///
unemp_prob profits ///
z_unemp_prob z_profits ///
northeast midwest south west ///
female age agesq age_18_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65 ///
below_highschool highschool some_college associate_degree bachelors_degree postgrad_degree atleast_bachelor ///
empl_ft empl_pt self_empl unempl out_laborforce retired student empl_other empl ///
income log_income inc_15000 inc_15000_25000 inc_25000_50000 inc_50000_75000 inc_75000_100000 inc_100000_150000 inc_150000_200000 inc_200000 inc_NR

keep $vars
order $vars

cd "$path_data_processed"
saveold "robustnessexperiment3.dta", v(13) replace























